> > |
META TOPICPARENT |
name="Slides" |
Slide 1: Accessing sensors and actuators
Session 2 of the AFNOG tutorial on IoT sensors
Uli Raich
Slide 2: The sensors and actuators
For our experiment we need 3 different devices:
- an rgb LED
- a color sensor
- a distance sensor
The rgb LED is not needed for the final project but very useful on the way to it
Slide 3: Choosing the rgb LED
rgb LEDs consist of 3 individual LEDS in red,green and blue
Different rgb LEDs are available on the market:
|
|
|
|
WS2812 |
SMD rgb LED |
LilyPad LED |
Standard LED |
Slide 4: WS2812
The WS2812 is an LED with a controller chip on board. It is used in LED chains where
each LED is individually addressed.
A precisely timed digital communication protocol is used to accessed the LED.
Too complex for our tutorial
Slide 5: The SMD rgb LED
This is a surface mount LED on a small PCB. It has 4 pins:

Unfortunately there are no current limiting resistors on this PCB which must
be provided externally (which values if we want max. 10mA in one LED?)
Slide 6: The KY-016 rgb LED
The standard rgb LED on an BY-016 board has the current limiting resistors already on board.
The board uses a common cathode circuit, which means that it has a common ground
and the rgb signals must be on Vcc to light the LED
Slide 7: The LilyPad LED
The LilyPad LED also has the resistors already on board but uses a common anode LED.
This means that the common pin is on Vcc and the rgb pins
must be brought to ground to light the LED
Slide 8: Connection to the Raspberry Pi
The photo shows the connections of an LED to the Raspberry Pi by
means of the cobbler (blue PCB) and a flat cable.
Like this we get access to interface pins on the Broadcom BCM2837 processor chip.
Slide 9: GPIO
GPIO or General Purpose I/O pins are pins for a single signal that
can be programmed to be input or output.
In addition internal pullup resistors can be enabled if desired.
In order to light our LED we therefore need 3 GPIO pins, one for each color.
These pins must be programmed to be output and the corresponding signal level
must be set (active high in case of common cathode, active low in case of common anode).
The intensity can be changed using pulse width modulation (pwm)
where a high frequency signal is sent to the LED.
The intensity is changed by changing the duty cycle of the signal
Slide 10: Accessing GPIOs
Of course it is possible to access the GPIOs through
register access on the processor chip.
This however is clumsy and needs detailed knowledge of the chip.
A library is available giving the programmer easy access to GPIOs,
including intensity variation through pwm.
The library we will be using is pigpio which has a Python interface.
The library uses a daemon (pigpiod), which is usually started at system startup.
It is also possible to start it manually as super user.
The Python module connects to the daemon and sends commands to it.
The daemon is responsible for the hardware access.
Please have a look at the pigpio docs .
Slide 11: The PIGPIO documentation
Slide 12: Accessing the LED
Before trying to access the LED by program we simply connect
- Vcc to the red, green or blue pin and GN to ‘-’ in case of the common cathode PCB
- Vcc to ‘+’ and GND to red, green or blue in case of the common anode PCB
This will light the corresponding color or the LED.
Then we connect the LED to the Raspberry Pi as follows:
- red to pin 18
- green to pin 17
- blue to pin 22
- ‘-’ to GND in case of the common cathode version or
‘+’ to Vcc in case of the common anode version
Slide 13: Lighting the LED by program
Strategy:
- First we import the pigpio library:
import pigpio # imports the library
- Create a pi object from the pigpio class
This will connect our program to the pigpiod daemon
pi = pigpio.pi()# connects to the daemon
- We program the gpio pins to be output:
pi.set_mode(_RED,pigpio.OUTPUT) # _RED must be set to 18
- And finally we write 0 or 1 to the pin:
pi.write(_RED,pigpio.HIGH) # or pigpio.LOW
- In order to change the intensity of the color component we use pwm:
pi.set_PWM_dutycycle(_RED, intensity) # intensity: 0..255
Slide 14: 8 Colors
if we switch each of the LEDs then, because of 3 pins, we have 8 different colors.
All pins off is black, all pins on is white.
Slide 15: Remotely connect to pigpiod
The call to pigpio can have up to 3 parameters:
pi = pigpio.pi(host,port,show_errors)
After this the program is identical to what we have seen. So…
What is the difference?
In the first case both, pigpiod and the Python program run on the Raspberry Pi
and only the keyboard, mouse and screen are used to interact via the ssh interface.
In the second case the Python program runs of the PC
and only the I/O to the sensors or LEDs happen through the daemon.
Slide 16: Shades of Colors
To see 255 different shades of red, this is what we need to do:
Of course you can do the same thing for green and blue shades
or for any combination of the colors
Slide 17: Intermediate Colors, PWM
How can we get a finer definition of colors is each of the LEDs
can only be driven by binary signals?
The answer is pwm: pulse width modulation.
The signal to the LEDs is sent a a high frequency where the switching is invisible to the human eye.
The duty-cycle determines the current flowing through the LED and thus the color.
It can easily be seen that with respect to a permanent “1”
a pwm signal with 50% duty-cycle produces on average only ½ of the current
Slide 18: Color and Distance Sensor
Reading the HC-SR04 ultra-sonic distance sensor and the TCS3200 color sensor
is much more complex than driving the LEDs
However, we are lucky:
There are Python classes supporting these devices written for us by experienced Python programmers.
These classes come with example code showing how to use the class methods.
Of course we can just use the code. It is however much better to try and understand the code before using it.
Reading code written by expert programmers can help us to substantially improve our own programming skills.
Slide 19: The Ultra-Sonic Distance Sensor
We can consider the sensor as a loudspeaker and microphone. These are the two cylinders.
On a trigger the sensor sends an 8 cycle burst of ultra-sound (40 kHz)
It then reads the echo and generates a pulse whose length is proportional to the signal travel time.
Slide 20: The HC-04 distance sensor
Slide 21: HC-SR04 Signal Connections
Vcc |
5V |
Trig |
GPIO 5 |
Echo |
GPIO 6 via level converter |
GND |
GND |
We create a 10 μs pulse on the Trig pin, setting gpio pin 5 to HIGH,
wait for 10 μs and setting it low again.
Then we read the time at which the Echo signal becomes high and when it becomes low again.
The time difference corresponds to the sound travel time.
Since we know that the
speed of sound in air is ~ 343 m/s
the distance the sound wave has traveled is distance = 34300/2 * signalLength [cm]
(the factor 2 comes from the fact that the wave travels from the loudspeaker
to the paper and back to the microphone, which is twice the distance)
Slide 22: Level converter
The HC-SR04 and the TCS3200 work on 5V with 5 V logic levels while
the Raspberry Pi works with 3.3V logic levels.
This is ok for Raspberry Pi output signals. The correct logic level is seen by the sensors.
However, the input signals to the Raspberry Pi must be converter to 3.3V,
which can be done with a resistor voltage divider or with a level shifter:
On the B side we provide 5V signals which are converted to 3.3V on the A side
Both sides need Vcc (side B: 5V, side A: 3.3V) and GND
Slide 23: The HC-SR04 code, init
Slide 24: The HC-SR04 code, loop
Slide 25: Timeout
There is a problem with the code:
Raspbian is a multi-tasking system, treating a large number of interrupts.
What can happen when an interrupt is treated while our program is waiting for a transition?
We may wait forever! The program gets blocked!
There are 2 ways to treat this:
- use timeouts: make sure that the time since we started waiting
for the transition does not exceed a predefined max. value
- use callbacks
-- Uli Raich - 2018-04-25
Comments
META FILEATTACHMENT |
attachment="lilyPad.png" attr="" comment="" date="1524661547" name="lilyPad.png" path="lilyPad.png" size="159018" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="smdRgbLed.png" attr="" comment="" date="1524661554" name="smdRgbLed.png" path="smdRgbLed.png" size="156061" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="standardLed.png" attr="" comment="" date="1524661560" name="standardLed.png" path="standardLed.png" size="155241" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="ws2812.png" attr="" comment="" date="1524661565" name="ws2812.png" path="ws2812.png" size="161580" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="commonAnode.png" attr="" comment="" date="1524661646" name="commonAnode.png" path="commonAnode.png" size="24404" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="commonCathode.png" attr="" comment="" date="1524661646" name="commonCathode.png" path="commonCathode.png" size="24217" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="cobbler.png" attr="" comment="" date="1524662415" name="cobbler.png" path="cobbler.png" size="24360" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="cobblerPinout.png" attr="" comment="" date="1524662427" name="cobblerPinout.png" path="cobblerPinout.png" size="288280" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="cobblerBreadboard.png" attr="" comment="" date="1524662657" name="cobblerBreadboard.png" path="cobblerBreadboard.png" size="225499" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="pigpioDoc.png" attr="" comment="" date="1524663148" name="pigpioDoc.png" path="pigpioDoc.png" size="300030" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="eightColors-1.png" attr="" comment="" date="1524663662" name="eightColors-1.png" path="eightColors-1.png" size="19992" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="eightColors-2.png" attr="" comment="" date="1524663662" name="eightColors-2.png" path="eightColors-2.png" size="36339" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="shadesOfColor.png" attr="" comment="" date="1524663952" name="shadesOfColor.png" path="shadesOfColor.png" size="36473" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="pwm.png" attr="" comment="" date="1524664279" name="pwm.png" path="pwm.png" size="67213" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="distanceSensor.png" attr="" comment="" date="1524664558" name="distanceSensor.png" path="distanceSensor.png" size="185051" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="hc-sr04Timing.png" attr="" comment="" date="1524664651" name="hc-sr04Timing.png" path="hc-sr04Timing.png" size="94177" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="hc-sr04Init.png" attr="" comment="" date="1524665190" name="hc-sr04Init.png" path="hc-sr04Init.png" size="42932" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="hc-sr04Loop.png" attr="" comment="" date="1524665192" name="hc-sr04Loop.png" path="hc-sr04Loop.png" size="33513" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="levelConverter.png" attr="" comment="" date="1524665200" name="levelConverter.png" path="levelConverter.png" size="166033" user="UliRaich" version="1" |
META FILEATTACHMENT |
attachment="timeout.png" attr="" comment="" date="1524665478" name="timeout.png" path="timeout.png" size="10295" user="UliRaich" version="1" |
|