Author Archives: teel9174

I2C Communications between Raspberry Pi and Arduino – Part Two

The goal of the day is to get the Raspberry Pi and Arduino talking to each other over I2C.

I followed a few examples provided on the internet and was able to get the two to talk to each other. Just to be clear, the Raspberry Pi will be the I2C master and the Arduino will be the slave. One of the nice advantages to this configuration is that it is not necessary to do any voltage level shifting between the two devices. If you are not aware, the Raspberry Pi GPIO is at 3.3V and the Arduino is at 5V. If the Arduino were to supply 5V to any of the Raspberry Pi’s GPIO pins, the Raspberry Pi will be toast.

I followed the tutorial at http://blog.retep.org/2014/02/15/connecting-an-arduino-to-a-raspberry-pi-using-i2c/. Below are some of the high level steps.

  1. Download the latest Raspbian image from https://www.raspberrypi.org/downloads/raspbian/
  2. Unzip the file and write the image to the SD Card using Win32DiskImager from https://sourceforge.net/projects/win32diskimager/
  3. Once the Raspberry Pi boots, open a terminal window and run raspi-config to enable I2C Support
    sudo raspiconfig
    Config_I2C_000
  4. Select “Advanced Options” from the menu
    Config_I2C_001
  5. Select “I2C” from the Advanced Options menu
    Config_I2C_002
    Select “Yes”
    Config_I2C_003
    Select “OK”
    Config_I2C_004
    Select “Yes”
    Config_I2C_005
    Select “OK”
    Config_I2C_006
    ”Select “Finish”
    Config_I2C_007
  6. Install i2c-tools
    sudo apt-get update
    Config_I2C_011
    sudo apt-get install i2c-tools
    Config_I2C_012
  7. Run i2cdetect to make certain that i2c-tools installed properly
    i2cdetect –y 0
    or
    i2cdetect –y 1
    Config_I2C_013
    If all worked well, you will see the following output
    Config_I2C_014
    If there are devices connected to the I2C pins, you will see the devices listed as in this example.
    Config_I2C_015
  8. Wire up the Arduino and Raspberry Pi
    Raspberry PI        Arduino
    GPIO 0 (SDA)    <–>    Pin 4 (SDA)
    GPIO 1 (SCL)    <–>    Pin 5 (SCL)
    Ground    <–>    Ground
    Step002_bb
    Step002_schem
  9. Upload Code to the Arduino
  10. Write the application on the Raspberry Pi
    nano testi2c02.c
    Config_I2C_020
  11. Type of copy paste the following code
  12. Save the file by pressing <Ctrl> + o

    Config_I2C_022
    Config_I2C_023
  13. Exit the editor by pressing <Ctrl> + x
    Config_I2C_024
  14. Compile the application
    gcc testi2c02.c -o testi2c02
    Config_I2C_025
    If you do see errors, go back and edit the file to correct them.
    Config_I2C_025
    Once the changes are made, recompile and if you do not see any error messages, you are good to go.
    Config_I2C_026
  15. Run the application
    ./testi2c02 1 {Gets the temperature in Celsius}
    Config_I2C_030
    ./testi2c02 2 {Gets the relative humidity in percent}
    Config_I2C_031

    ./testi2c02 3 {Gets the light level}
    Config_I2C_032

    ./testi2c02 4 {Turns the LED On}
    Config_I2C_033

    ./testi2c02 5 {Turns the LED Off}
    Config_I2C_034

    ./testi2c02 6 {Flashes the LED when reading sensors. This is the default behavior of the LED}
    Config_I2C_035

    ./testi2c02 7 {Echos the number 7. This may be repeated with any other number up to 255}
    Config_I2C_036

    Config_I2C_037

That’s all for the day. Next I plan to try to send strings and develop a format for messages.

I2C Communications between Raspberry Pi and Arduino – Part One

My daughter has a science project for school on bread mold growth. She will need to monitor the temperature, humidity, and light level of 3 separate environments. Being the geeky dad that I am, I decided to make her some data loggers to monitor each environment. I would also like to take this further by connecting to an online IoT site such as adafruit.io to store and graph the data. There are a few options available such as using an Arduino with a Wi-Fi shield to connect to the site and monitor the environment but that is not an elegant solution. What I have opted to do is to use a Raspberry Pi instead and use I2C to communicate to the sensors using ATTiny85 microcontrollers. One of the reasons for this choice was that she will need to monitor the growth with 10 to 30 slices of bread for each environment. With that many slices in one batch,  there could be a considerable variation throughout the area containing the bread so more than one data logger/sensor cluster should be used. I2C is the perfect solution as you may have up to 127 devices connected with just 3 wires.

Use an Arduino Uno R3 to get information from the sensors and verify that the code works correctly.

Step001_bb

Step001_schem

Arduino Code

Output

 

 

Next Step -> Add Raspberry Pi and I2C Communication

Portable PI – Power Supply and Power Switch

Finally nearing the end of completing the power supply and soft latching power switch. The components have changed slightly but the principle design is the same. The components include two li-ion batteries connected in parallel with protection circuit, soft latching switch, Adafruit Powerboost 1000 with charging circuit (Product #2465) , and a Teensy 3.1. Along with the hardware components, there are two software pieces with one residing on the Teensy and the other running in the background of the Raspberry Pi.

The design utilizes a soft latching power switch has three functions. It powers the device on from an off state, signals the Teensy that the user has pressed the power button to request a shutdown, and power off. One of the main requirements besides those mentioned above is that the soft latching power switch must not draw current or very little current when in the off state. This is necessary as we do not want the batteries to be drained when the device is in the off state.

These requirements were met with a modified version of the soft latching power circuit described in my earlier post. The circuit was modified for a couple of reasons. First of all, the circuit needed to be able to operate from 3 to 4.5 VDC rather than at 5 VDC. Another reason the circuit needed to be modified is that the output was floating when off so the enable signal to the powerboost circuit would cause the power to cycle back on after a few seconds.

There are two pieces of software working together which allows the Raspberry Pi to safely shutdown when the user presses the power button. The Teensy has software which monitors the power button to see if it is pressed and monitors the USB power from the Raspberry Pi. If the user presses the power button, the Teensy changes the state of a pin on the Raspberry Pi. The Raspberry Pi has a program running in the background which checks if the logic level on the pin has changed. If the logic level changes, the script will issue a shutdown command.

The next thing was to determine the best way to know when the shutdown was complete.

Soft Latching Power Switch

The Raspberry Pi ON/OFF Power Controller that I looked at from Mosaic Industries, Inc. does a good job at 5 VDC but I need the circuit to be able to switch LiPo batteries which have a voltage range of between 2.7 and 4.35 VDC between discharged and fully charged. I looked for dual MOSFET devices which could operate in this range and found the following.

Both of these devices function at these lower voltages however I have been having some oscillation issues with these. When the button is held down to turn off the load, which is a LED, the output is switched off but it turns back on after a few seconds. I have not been able to investigate why this is happening. I suspect that I miswired something but I need to take some time to check.

The International Rectifier device seems to behave a bit better so it may be the one I use but I will need to investigate what the issue is and resolve that before I can move on. I can’t wait to wrap this project up.

If you have an idea as to what my problem may be, please leave a comment to let me know.

UPDATE – 16 May 2015

I determined the reason for the odd behavior was that the N-Channel MOSFET’s gate was floating. I was able to fix this by using a pull-down resistor of 100KΩ. Looking at the schematic, I connected the resistor between Pin 7 and ground. This pulls the gate to ground through the 330KΩ and 1KΩ resistors.

Weekly update

Dealing with PC issues at the moment so this week has been a bit rough.

My inspiration for the Portable Raspberry Pi came from the Ben Heck Show on Ben’s creation of a Portable Pi. (Part 1 & Part 2) I liked Ben’s design but there were a few things that I wanted to change.

  1. I did not want to deal with having a batteries in series and not be able to easily charge them in the case without disconnecting them from the device. This requirement was due to Ben’s choice of LCD monitor so I am changing the LCD to an Adafruit PiTFT screen which uses SPI. This will be a bit trickier to use but should not be a problem.
  2. I wanted to use the powerful Teensy for more than just the gamepad so I modified the Teensy code to provide a custom gamepad which has only the buttons that I will have plus provides keyboard and raw HID devices. The reason for these additional devices are for power management and control.
  3. Power – I wanted to be able to charge the battery in the device and still be able to use the device while it was charging. To implement this requirement, I turned to Adafruit’s USB LiIon/LiPoly charger.
  4. Power On/Off – The Portable Pi will be used by my children so I wanted to have an easy way for them to shut it down as well as to automatically shut it down when the batteries are low. This is where the additional Keyboard and Raw HID device come in on the Teensy. The plan is to implement a soft latching power switch circuit which can turn on the power to the device when pressed from an off state. When it is on, the Teensy will be able to detect when it is pressed and start an orderly shutdown of the Raspberry Pi. If the button is held down longer, about 3 seconds, then the power will be switched off. This will allow it to function much like the power switch on a PC or laptop.
  5. Power monitoring – I would also like to be able to monitor the battery level and perform an automatic shutdown of the Raspberry Pi if the voltage is too low. This is where the Raw HID device comes in. I plan to use the Teensy to monitor the battery voltage and send periodic updates to the Pi to let it know the level. If the level reaches a min threshold then it will send keypresses through the keyboard device to tell the Pi to shutdown.

These are just a few of the high level requirements I have for the Portable Pi. So far, I have the Gamepad code completed and the hardware on a breadboard. I am currently working on the soft latching power switch. I have built a few circuits which work but only at 5 VDC. I need it to work at 3 VDC and higher so I need to order some parts as I do not have MOSFETs which will switch full on at that low of a voltage. I plan to implement a design found on Mosaic Documentation Web. I have built a similar circuit and found it to work well and meet the requirements that I have. I also looked at David Jones’s design on his EEVBlog but it did not meet all of the requirements that I have. It is a nice simple design so I am certain I will use it in the future on another project.

Updates and Future Plans for this Blog

Well, I have done a terrible job keeping this blog up to date so I will try once again to do a better job. I plan to update this blog at least once a week by adding an update even if I feel there is not much to report. The plan is to do a blog entry each Monday by 10 PM EST.

Some updates since my last blog post.

I have managed to be on Adafruit’s Show and Tell a few times. Adafruit’s Show and Tell is hosted by Google Plus Hangouts.

  • 18 Jan 2014: Pinewood Derby Car
  • 26 Nov 2014: Guggenhat with mods
  • 31 Dec 2014: LED Matrix Clock and Eiffel Tower Alarm Clock
  • 11 March 2015: Power Supply

I am currently working on a Portable Pi. It is a slightly different design than any of the others I have seen posted online. Most likely the next few posts will be regarding the build of the Portable Pi. Once that is completed, I plan to post a few of the builds mentioned in the Adafruit Show and Tell segments such as the power supply, Guggenhat mods, and LED Matrix Clock mod.

Raspberry PI B+

Hardware

Needed

  • Raspberry Pi
  • MicroSD Card 4GB or larger
  • USB Keyboard
  • USB Power Supply and cable with micro USB Connector
  • Display (Composite or HDMI)
  • Display cable (Type of cable depends on monitor)
  • MicroSD Card to SD Card Adapter (Needed only if your PC has a SD Card Reader but not a MicroSD Card Reader)

Nice to Have

  • Raspberry Pi B+ Case (Note cases for other models may not work)
  • WiFi module or Ethernet Cable

Steps

  1. Download the Raspbian Image from http://www.raspberrypi.org/downloads/ (As of this post, the file is 2014-06-20-wheezy-raspbian.zip)
  2. Unzip the Raspbian Image File
    Image2
  3. Follow the instructions at http://www.raspberrypi.org/documentation/installation/installing-images/README.md for your operating system. I will be using Windows and will mirror the steps from the installation instructions page

Windows Installation

  1. Insert your MicroSD card into the card reader on your PC (NOTE: You may need a MicroSD to SD Card Adapter)
  2. Download the Win32DiskImager utility from http://sourceforge.net/projects/win32diskimager/ (As of this post, the file is Win32DiskImager-0.9.5-install.exe)
  3. Install the Win32DiskImager Utility by double clicking the downloaded file
  4. Click on the “Next >” button on the setup wizard
    Welcome to the Win32DiskImager Setup Wizard
  5. Select the “I accept the agreement” radio button and click the “Next >” button
    License Agreement
  6. Click the “Next >” button on the destination location dialog
    Select Destination Location
  7. Click the “Next >” button on the Select Start Menu Folder dialog
    Select Start Menu Folder
  8. If you do not want a desktop shortcut, uncheck the “Create a desktop icon” checkbox
  9. Click the “Next >” button on the Select Additional Tasks dialog
    Select Additional Tasks
  10. Click the “Install” button on the Ready to Install dialog
    Ready to Install
  11. Wait for the installation to complete
    Installing
  12. Click the “Finish” button on the Completing the Win32DiskImager Setup Wizard dialog
    Completing the Win32DiskImager Setup Wizard
  13. It is possible that you may see the following error message
    Unable to execute file
  14. If you see the error message above
    1. Click the “OK” button
    2. Right-click on the desktop shortcut and select “Run as administrator” from the pop-up menu
      Run as administrator
    3. When the User Access Control dialog is displayed, click the “Yes” button
      User Access Control dialog
  15. The application is now open
    Win32 Disk Imager
  16. Click the folder icon and select the Raspbian image file downloaded earlier
    Selecting the image file
  17. Select the drive letter for your MicroSD Card
    WARNING: Be very careful to select the correct drive letter as the utility will erase all data on the drive! You have been warned!
    Select the MicroSD Card
  18. Click the “Write” button
    Write the image
  19. Click the “Yes” button on the confirmation dialog
    Confirm Write
    If you see the following error (), check that there are no open Explorer Windows or other applications attempting to access the SD Card
    Write Error
    If you see the following error, check the write lock switch on the MicroSD Card Adapter
    Lock Error  SD Card Lock Switch
    Wait for the process to complete
    Wait
  20. When the process is complete, click the “OK” button
    Write Sucessful
  21. The main window will display the status as “Done.” Click the “Exit” button to exit the utility
    Done.
  22. Eject the MicroSD Card

Once the Raspbian Image has been written to the MicroSD Card, you are ready to boot and configure the Operating System (OS).

Booting and Configuring the Raspberry Pi using Raspbian OS

  1. Insert the MicroSD Card in the Raspberry Pi
  2. Connect the monitor, keyboard, mouse (optional), and power
  3. The Raspberry Pi Software Configuration Tool (raspi-config) will launch

A look at Adafruit’s Electret Microphone Amplifier – MAX4466 with Adjustable Gain (1063)

I purchased Adafruit’s Electret Microphone Amplifier – MAX4466 with Adjustable Gain (1063) and wanted to incorporate the circuit into a board that I am making to make a sound responsive decorative light. I saw a post from Adafruit that stated that the schematic was taken directly from the datasheet however looking at the component values on the board, it was obvious that some values had been changed. After a bit of reverse engineering of the board, it was confirmed that the schematic is indeed Figure 2 from the datasheet.

Figure2

Board Layout

Component Listing (Note Capacitor Values taken from Datasheet Schematic and not verified)

      • IC1 – MAX4466
      • R1 – 1KΩ
      • R2 – 1KΩ
      • R3 – 1MΩ
      • R4 – 1MΩ
      • R5 – 1KΩ
      • R6a – 500KΩ
      • R6b – 22KΩ
      • C1 – 0.1µF
      • C2 – 0.01µF
      • C3 – 1µF
      • C4 – 100pF
      • L1 – Ferrite Bead
      • L2 – Ferrite Bead

I wrote a simple program on the Arduino to capture 100 points of data per second for one minute and write the results to the serial output.

Here is the circuit diagram for the first test with values from the datasheet.

TestSchematic01

Test Schematic 1 – Values from datasheet

At first, I tried to use the values from the datasheet and noticed that the gain was not as high as the Adafruit module.

Test01_Chart01

Adafruit (ID: 1063) and MAX4466 Typical Schematic

Test01_Chart02

Adafruit (ID: 1063) Capture

Test01_Chart03

MAX4466 Schematic from Datasheet

Modified values to be close to the values used by the Adafruit 1063 module. The results showed that the gain was much better so these values will be good enough for my application, If you need to use this for a different application where you need better performance, you may want to look into the RC values. By modifying the capacitor values, it may be possible to achieve a cleaner output.

TestSchematic02

Modified resistor values to match Adafruit 1063 module

Test02_Chart01

Adafruit (ID: 1063) and MAX4466 Typical Schematic modified values

Test02_Chart02

Adafruit (ID: 1063)

Test02_Chart03

MAX4466 Typical Schematic modified values

This is not a thorough analysis of the performance of these two circuits but as stated above, it was good enough to give me the performance that I needed. (Test with Datasheet Figure 2)

BTW: The values read by the Arduino ADC are between 0 and 1023. The signal is centered around 511 and  has a noise level of ±50.

FYI: The gain for the circuit may be calculated as:

Gain = Rf/Rin = R6/R5 = 100KΩ/10KΩ = 10 (Dtasheet Circuit)

Gain = Rf/Rin = R6/R5 = 120KΩ/1KΩ = 120 (Modified Circuit)

You can also see from the graphs that the amplifier is driven to saturation. For my application that is acceptable but it may not be acceptable for your application particularly if you are creating a purely audio application.

Arduino Due Issues

I have been working with the Arduino Due and version 1.5.2 of the Arduino IDE. I have been attempting to port a mini digital picture frame that I wrote for the Arduino Micro and have not been having much luck. The problem is that the Arduino Due is still relatively new and is a different family than the previous Arduinos.

The errors that have been encountered due to references to the avr libraries. (In particular avr\io.h and avr/pgmspace.h.)

In my attempt, I did the following:

  1. Created the program shown below to workout the library issues. Once I am able to resolve the library issues, I will be able to start porting the code.
  2. Replace the Adafruit_ST7735 library with the modified version at https://github.com/sngl/Adafruit-ST7735-Library” target=”_blank which has been modified to support the Arduino Due
  3. Update the Adafruit_GFX library with the changes found at https://github.com/elechouse/Adafruit-GFX-Library/commit/69e355325312a57412d8c4d0ec6298aa3b4ed917
  4. Create a new folder in the Arduino IDE location at hardware/arduino/sam/cores/arduino/avr/
  5. Create a new file at hardware/arduino/sam/cores/arduino/avr/pgmspace.h and enter contents found at https://github.com/arduino/Arduino/commit/0f5a5259ec038537ea51de0059e711fdeebc6ece

Some issues are resolved but others remain.

Code

#include <Adafruit_GFX.h> // Core graphics library // OK after changes to libraries
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h> // OK
// #include <SD.h>

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}


Errors

In file included from sketch_may19a.ino:20:
C:\arduino-1.5.2\libraries\Adafruit_ST7735/Adafruit_ST7735.h: In member function 'uint16_t Adafruit_ST7735::Color565(uint8_t, uint8_t, uint8_t)':
C:\arduino-1.5.2\libraries\Adafruit_ST7735/Adafruit_ST7735.h:116: error: 'newColor' was not declared in this scope

Commented line 20 in the Adafruit_ST7735.h file as the Color565 function does not appear to be called elsewhere and the newColor function does not exist in any libraries. The following errors are then produced.

C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::commonInit(uint8_t*)':
C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:266: error: cannot convert 'volatile RwReg*' to 'volatile uint8_t*' in assignment
C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:268: error: cannot convert 'volatile RwReg*' to 'volatile uint8_t*' in assignment
C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:276: error: 'SPI_CLOCK_DIV4' was not declared in this scope
C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:283: error: cannot convert 'volatile RwReg*' to 'volatile uint8_t*' in assignment
C:\arduino-1.5.2\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:285: error: cannot convert 'volatile RwReg*' to 'volatile uint8_t*' in assignment

Why is it when people provide fixes they are rarely documented completely? This is one of the most frustrating things that I encounter. I can solve these issues if given enough time but unfortunately I do not have the luxury. I will come back to this later and attempt to resolve the remaining issues.

Here is a post of someone else trying to get the Adafruit display to work with the Arduino Due. http://21stdigitalhome.blogspot.com/2013/02/arduino-due-spi-display-nearly-defeated.html

To be fair, the Arduino Due is a totally different architecture and it will take time to port the libraries over to the new architecture. I hope to get some more time later to tackle this problem again as this is a great opportunity to learn more about the Arduino Due but I suspect that I will not be able to devote the time I need to accomplish much.

Setting up the WiFi Part I – Supported Adapters

Video Coming Soon

This entry is for configuring the Asus USB-N10 USB Wireless-N USB Adapter. If you have any other WiFi adapter please refer to Chapter 4 Network Configuration of the Raspberry Pi User Guide book or perform Google Searches for your adapter.

003_001

NOTE: There is a WiFi Config tool included with Raspbian “wheezy” distribution however I have not had much luck with it. I t did work one time for me and that was it. You may give it a try but I recommend using the steps below as they have worked 100% of the time for me.

 

You will need to perform these steps from the command line. If you have the desktop loaded, open a terminal window by double clicking on the the LXTerminal shortcut on the desktop.

  1. At the command line, type the following command and press enter to view available wireless networks.
    sudo iwlist scan | less
  2. On the wlan interface, you will see the SSIDs listed on the lines starting with ESSID:. Verify that the network you wish to connect to is listed.
    003-002
  3. Press Ctrl+z to return to the command line
  4. Edit the network configuration file by typing the following command and press enter.
    sudo nano /etc/network/interfaces
  5. You should see the following content in the interfaces file.
    auto loiface lo inet loopback
    iface eth0 inet dhcpallow-hotplug wlan0
    iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp

     

  6. Replace the bottom block with the following.
    auto wlan0
    iface wlan0 inet dhcp
    wpa-conf /etc/wpa.conf

     

  7. The resulting file should have the following contents.
    auto loiface lo inet loopback
    iface eth0 inet dhcpauto wlan0
    iface wlan0 inet dhcp
    wpa-conf /etc/wpa.conf

     

  8. Save the file and exit
    • Press Ctrl+o
    • Press Enter
    • Press Ctrl+x
  9. Now edit the wpa.conf file by typing the following command and pressing enter.
    sudo nano /etc/wpa.conf
  10. Enter the following for the contents of your file based on the type of security used by your WiFi network.
    NOTE: Do not type the word [Tab]. It indicates that you need to include a tab.

    No Encryption network={
    [Tab] ssid=”Your_SSID
    [Tab] key_mgmt=NONE
    }
    WEP Encryption network={
    [Tab] ssid=”Your_SSID
    [Tab] key_mgmt=NONE
    [Tab] wep_key0=”Your_WEP_Key
    }
    WPA/WPA2 Encryption network={
    [Tab] ssid=”Your_SSID
    [Tab] key_mgmt=WPA-PSK
    [Tab] psk=”Your_WPA_Key
    }

     

  11. Save the file and exit
    • Press Ctrl+o
    • Press Enter
    • Press Ctrl+x
  12. Connect to the wireless network by typing the following command and pressing enter.
    sudo ifup wlan0
  13. If you see the message “ifup: interface wlan0 already configured,” type the following command and repeat the previous step again.
    sudo ifdown wlan0
  14. If you are successful, you will see several lines starting with DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 … with the last line stating that the adapter is bound to an IP address on your network
    .
    003-003
  15. If you are successful at this point, you may close the terminal window and open a web browser to verify that you are connected to the internet.
    003-004