In this workshop, we will learn about PWM and I2C, and we will connect temperature/humidity sensors and accelerometers to our Raspberry Pi’s..
PWM stand for Pulse Width Modulation. The DHT22 Temperature/humidity sensor uses PWM to communicate with your Raspberry Pi. In other protocols, logic is transferred in binary format, with a high state (+ voltage) representing logical ‘0’ (most of the time) and low state (0 voltage) representing logical ‘1’.
With PWM, logical ones and zeros are represented by the duration of signals sent, and not their state (low or high). In the PWM the DHT22 uses, a high (natural) state signal lasting between 26-28us (microseconds) represents a logical zero. A high (natural) state lasting 80us (microseconds) represents a logical 1. 50us (microsecond) low states are inserted between high state bits to delimit them.
The DHT temperature/humidity sensor reports readings in a 40 bit stream, as represented by the following timeline…
As shown in the timeline, The first 8 bits of the signal are the whole number part of the humidity reading. The second 8 bits are the decimal part of the humidity reading. The third 8 bits are the whole number part of the temperature reading. The fourth 8 bits are the decimal part of the temperature reading. The last 8 bits are a checksum used by code to validate the reading. Those 8 bits represent the last 8 bits of the sum of the previous 32 bits.
I2C stands for Inter-Integrated Circuit. The MMA7660 accelerometer uses I2C protocol to communicate with the Raspberry Pi. I2C differs from PWM in that it allows multiple devices to communicate over the same channel, called an I2C bus. Each device on an I2C bus is required to have a unique address. Then a centrally located controller, in our case a Raspberry Pi, can communicate with all devices individually using their unique addresses, as shown in the I2C bit frame shown below.
The MMA7760 accelerometer has 5 pins. For this experiment, we will only be using 4 of them. Two of the pins are used for power(VCC) and ground(GND). The third pin we will be using is for signal(SDA), and the fourth is for a clock(SCL) signal pulse, generated by the Raspberry Pi.
As seen in the bit frame diagram above, when a controller (Raspberry Pi) wishes to communicate with an I2C device on the bus, it will generate and send a frame containing the address of the device on the bus (7 bits), followed by a read/write bit(1 bit), and then, if it’s a write, data to be written to the device (8 bits).
- Log in to your internet connected Raspberry Pi using the following command (Linux)…
$ ssh pi@<your Pi's local ip address or hostname>
Windows users connect to your Pi using Putty.
- Once connected, you will need to current code for this workshop. We have our very own repository on GitHub. To get up to date code, issue the following command from your home directory on your Pi…
- This will download a copy of the Austin IoT code repository to your Pi and store it in it’s own sub-directory inside your home directory.
- For these experiments, you will need to have the following packages installed on your Pi. Run the following commands at the command line of your Pi. If you already have these packages installed, nothing bad will happen.sudo apt–get install build–essential python–dev python–openssl
- After installing these packages update your packages using the following commandsudo apt–get update
and wait for that to complete.
You now have all of the code you need to complete this workshop. Let’s start hooking things up!
Here is a reference image to the pinout of the Raspberry Pi. We will be using this when we connect our devices below
For a complete list of all parts used in our workshops please see http://austiniot.com/workshop-parts-list/
For this workshop, we will be doing it in two pieces. First, we will connect the DHT-22 temperature/humidity sensor and run that code. Then we will connect the MMA7660 accelerometer, and run that code.
DHT-22 Temp/Humidity Sensor
For these steps, you will need to following parts…
|Raspberry Pi (2 or 3)||1|
|Temperature Sensor (DHT-22)||1|
|Resistor – 10k||1|
|DuPont Jumper – Male To Male||3|
|DuPont Jumper – Male To Female||3|
The pinout for your DHT22 is shown below…
For the next steps, you will need a 10k resistor to complete your circuit. If you do not have one. please see the instructor and one will be given to you.
Power off your Pi and let it shut down. Connect pins 1,2, and 4 as shown below. Pin 3 is not used.
Pin 1 -> +3.3v power (pin 1 on the Raspberry Pi header).
Pin 2 -> Pin 7 on Raspberry Pi header.
Pin 4 -> Any ground on Raspberry Pi header
Also, in between pin 2 of your DHT-22 and the lead that goes to pin 7 of the Pi, place the 10k resistor, with one end connected to pin 2 of the DHT-22, and the other end connected to positive 3.3v.
A picture of my completed DHT-22 circuit is shown below.
Go to /Austin-IoT-Workshop/Workshop04/Adafruit_Python_DHT-master and run
sudo python setup.py install
Then navigate to the examples folder by executing:
To run the example on a Raspberry Pi with a DHT-22 sensor connected to GPIO #4, execute:
sudo ./AdafruitDHT.py 2302 4
Make sure to run the command as root with the sudo command as shown above or else the program will fail to run (root access is required for reading and writing the GPIO pins).
You should now see the current temperature and humidity recorded from your sensor displayed on your screen. Remember, that temperature is in Celcius, so conversion will be necessary. (Extra creadit anyone?)
This program so not loop. As a result, every time you want to update your readings you will need to execute the command again.
A note about that command. In the line to execute the python script, the DHT-22 appears to be referred to as a 2302. That’s because Adafruit sells a DHT-22 sensor with wire leads connected to the pins, and that model number is 2302. For the purposes of this demonstration though, both are equal. We’re just using the code from the other one.
This concludes the DHT-22 portion of this lab. Lets move on now to the MMA7660 accelerometer.
MMA 7660 Accelerometer
To use I2C functionality on your Raspberry Pi, I2C must first be enabled in your Pi’s configuration. If you haven’t done this already, follow the steps below to enable I2C on your Pi.
At the command line of your Pi, type
sudo raspi-config, and hit enter.
From the choices that appear, use the arrow keys to highlight ‘Yes’ and press enter.
Upon accepting, you will next be asked if you wish to enable I2C by default. Use the arrow keys to highlight ‘Yes’ and press enter to accept. You should now see the following screen. Press enter to confirm.
It is important that you reboot your Pi in order for these changes to take effect. At your terminal command line, type sudo reboot now. This will disconnect your shell session, and your Pi will reboot. Wait about 30 seconds for your Pi to reboot, then connect back into it from your command line.
In order for you to manipulate your accelerometer using Python, one of the tools you must install with i2c-tools is a library called python-smbus. Smbus is a subset of I2C, and basically includes several C language wrappers around some Python functions. The smbus tools provide the link between your Python code, your I2C sensors, and your Raspberry Pi. More information on the smbus tools can be found at https://www.kernel.org/doc/Documentation/i2c/smbus-protocol. You are invited to explore these at your leisure.
If you havent already done this, install the python-smbus library using the command below. I you already have python-smbus installed, running this command again will not cause anything bad to happen.
sudo apt-get install python-smbus
What you have just done is installed code into your Pi’s Linux kernel that makes your Pi recognize I2C devices. To see this, change directories to /dev using the cd command (cd /dev). Once there, list the files in that directory using the ls command (ls -la). In the listing that appears, you should see a new device added, with a timestamp close to your current time called ‘i2c-1’. The Raspberry Pi can recognize up to one I2C bus, and that is the ‘-1’ part of that device name. If you see this you are a winner!
In order to get the most from I2C on your Raspberry Pi, there are several tools you can install that are freely available through the Apt repository. The first one is the package ic-tools. Install the ic-tools package onto your Pi as shown in the image below…
As you see, this has installed ic2 tools version 3.1.1.
Next, you can use one of these tools to detect any devices currently on your I2C bus. At the command line of your Pi, type i2cdetect -y 1 as shown in the image below. The ‘-y’ just tells the script to answer ‘yes’ to any prompts. The ‘1’ specifies which I2C bus on your Pi you are interested in.
What i2cdetect does is it tries to contact every possible address on your bus. Because I2C address space is only 7 bits, this can never amount to more than 127 possible addresses per bus. In the table above, the hexidecimal address of all I2C devices connected to the bus are shown.
But wait, you say, that table is empty. Indeed it is. That’s because we haven’t connected any devices yet. Let’s do that next.
For these steps, you will need to following parts…
|Raspberry Pi (2 or 3)||1|
|DuPont Jumper – Male To Female||4|
The pinout for your MMA7660 acclerometer is shown below. The pins on your MMA7660 should be labeled on the board silk screen as shown below.
Power off your Pi and let it shut down. Connect the pins as shown below. The INT pin is not used.
VCC -> +3.3v power (pin 1 on the Raspberry Pi header).
GND -> Any ground on Raspberry Pi header
SCL -> Pin 5 on Raspberry Pi header. (Labeled SCL in Raspberry Pi pinout diagram above)
SDA -> Pin 3 on Raspberry Pi header. (Labeled SDA in Raspberry Pi pinout diagram above)
Now power down your Pi by issuing a ‘sudo shutdown now’ command at the command line. Once your Pi has shut down, connect your MMA7660’s VCC, GND, SCL, and SCA pins as shown above. Once this is done, power your Pi back up and wait for it to finish booting.
When your Pi is back up, run i2cdetect again as shown below. If you did everything right, the hex address of your MMA7660 accelerometer on your Pi’s I2C bus should now appear. As you can see in the image below, in my case, it now appears at address ‘4c’ on the bus.
Once you get all of the pins connected to your Pi, power it up and wait for it to finish booting.
Go to the right directory and run the mma7660.py program using the following command. IMPORTANT: Make sure you type ‘sudo’.
sudo python mma7660.py
Note that this script will loop 1,000 times, and then exit.
What is an accelerometer? When you tap on your mobile phone’s screen, or rotate it from portrait to landscape orientation, you are using an accelerometer. Your phone has inside it a very small chip (about 1/8″square) that contains the necessary mechanism. When you think chip, you think electronics. But accelerators are actually mechanical in nature.
Some time ago, it was discovered that the etching process could be used to produce small pits in the silicon, with microscopic strips of semiconducting material over them. It was discovered that physical movement of these chips resulted in electrical variances that could be measured and quantified. Changes in motion and orientation of accelerometer chips, when they are powered, results in changes in voltage on the pins, that we can capture using our Raspberry Pi’s.