Accessing sensors and actuators
Session 2 of the AFNOG tutorial
on IoT sensors
Uli Raich
COPYRIGHT © 2025 by the contributing authors
Slide 1 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 2 of 25
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 |
COPYRIGHT © 2025 by the contributing authors
Slide 3 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 4 of 25
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?)
COPYRIGHT © 2025 by the contributing authors
Slide 5 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 6 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 7 of 25
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.
COPYRIGHT © 2025 by the contributing authors
Slide 8 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 9 of 25
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
.
COPYRIGHT © 2025 by the contributing authors
Slide 10 of 25
COPYRIGHT © 2025 by the contributing authors
Slide 11 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 12 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 13 of 25
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.
COPYRIGHT © 2025 by the contributing authors
Slide 14 of 25
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.
COPYRIGHT © 2025 by the contributing authors
Slide 15 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 16 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 17 of 25
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.
COPYRIGHT © 2025 by the contributing authors
Slide 18 of 25
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.
COPYRIGHT © 2025 by the contributing authors
Slide 19 of 25
The HC-04 distance sensor
COPYRIGHT © 2025 by the contributing authors
Slide 20 of 25
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)
COPYRIGHT © 2025 by the contributing authors
Slide 21 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 22 of 25
COPYRIGHT © 2025 by the contributing authors
Slide 23 of 25
COPYRIGHT © 2025 by the contributing authors
Slide 24 of 25
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
COPYRIGHT © 2025 by the contributing authors
Slide 25 of 25
--
Uli Raich - 2018-04-25
Comments
This topic: AFNOG
> WebHome >
Slides > AccessingTheSensorsAndActuators
Topic revision: r1 - 2018-04-25 - UliRaich

Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback