LcdStarterKit For Raspberry Pi
LcdStarterKit For Raspberry Pi
Lesson 8 Relay..................................................................................................................... 21
Lesson 15 LCD1602............................................................................................................. 37
0
Lesson 1 Blinking LED
Introduction
In this lesson, we will learn how to program your Raspberry Pi to make an LED blink. You
can play numerous tricks with an LED as long as your imagination is rich enough. Now
follow me to learn, and you will enjoy the fun of DIY at once.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*LED
- 1*Resistor (1KΩ)
- Jumper wires
Experimental Principle
Semiconductor light-emitting diode is a type of component which can turn electric energy
into light energy via PN junctions. According to its wavelength, semiconductor light-
emitting diode can be categorized into laser diode, infrared light-emitting diode and visible
light-emitting diode which, called light-emitting diode for short, is usually known as LED.
When we supply 2V-3V forward voltage to an LED, it will blink if only forward currents
flow through the LED. Usually we have red, yellow, green, blue and color-changing LED
which can change its colors with different voltages. LEDs are widely used due to its low
operating voltage, low current, luminescent stability and small size.
LEDs are diodes too. Hence they have a voltage drop which usually varies between 1V-3V
depending on their types. Likewise, LEDs usually can emit light if supplied 5mA-30mA
current, and generally we use 10mA-20mA. So when an LED is used, it is necessary to
connect a current-limiting resistor to protect the LED from over-burning.
1
In this experiment, we will connect a 1KΩ resistor to the positive pole of the LED and then
connect it to 3.3 V power source, and connect the negative pole of LED to GPIO0 (See the
Raspberry Pi pins distribution diagram and the above picture). If we write 1 to GPIO0, the
voltage of the pin is 3.3V and the LED will not blink; if we write 0 to GIIO0, the output
voltage is 0, then the LED will light up according to the above principle.
Experimental Procedure
Step 1: Connect the circuit as shown in the following diagram
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit/01_blinkingLed/led.c)
Further Exploration
If you want the LED to blink faster, just change the delay time. For example, change the
time to delay (200), recompile the program, and then run the program, you will see the
LED blinking faster.
Summary
Raspberry Pi packages many underlying detail designs, which enable us to explore our
own apps more conveniently. Maybe that is the most attractive place of Raspberry Pi.
Now you have already basically mastered the basic operation and application of Raspberry
Pi GPIOs (General Purpose Input/Output). I hope you can try harder and continue to learn
the next contents.
3
Lesson 2 LED Flowing Lights
Introduction
In this lesson, we will learn how to make eight LEDs blink in various effects as you want
based on Raspberry Pi.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 8*LED
- 8*Resistor (220Ω)
- Jumper wires
Experimental Principle
Set GPIO0 to GPIO7 to low in turn by programming, then LED0 to LED7 will light up in
turn. You can make eight LEDs blink in different effects by controlling their delay time and
the order of lighting up.
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram
4
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /02_8led/8Led.c)
./a.out
Now, when you press Enter, eight LEDs will light up circularly and render different effects.
Further Exploration
We can write the blinking effects of LEDs in an array. If you want to use one of these
effects, you can call it in the main() function directly.
5
Lesson 3 Controlling an LED by a Button
Introduction
In this lesson, we will learn how to turn an LED on or off by a button.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*LED
- 1*Button
- 1*Resistor (1KΩ)
- Jumper wires
Experimental Principle
Use a normally open button as an input of Raspberry Pi, when the button is pressed, the
GPIO (General Purpose Input/Output) connected to the button will turn into low level (0V).
We can detect the state of the GPIO connected to the button through programming. That
is, if the GPIO turns into low level, it means the button is pressed, you can run the
corresponding code according to this condition. In this experiment, we make the LED light
up.
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram
6
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /03_BtnAndLed/
BtnAndLed.c)
Step 3: Compile the code
gcc BtnAndLed.c -lwiringPi
Step 4: Run the program
./a.out
Press Enter, when you press the button, the LED will light up; when you release the
button, the LED will go out.
7
Summary
Through this experiment, you have basically mastered the Input and Output programming
operation of Raspberry Pi GPIOs. I hope you can make persistent efforts and continue to
learn the next contents.
8
Lesson 4 Tilt-Switch
Introduction
The tilt-switch we use here is a ball tilt-switch with a metal ball inside. It is used to detect
small angle of inclination.
Components
- 1*Raspberry Pi
- 1*Network cable (or USB wireless network adapter)
- 1*Tilt-switch
- 1*Resistor (1KΩ)
- 1*LED
- Several jumper wires
Experimental Principle
The principle is very simple. It mainly uses the ball in the switch changing with different
angle of inclination to achieve the purpose of triggering circuits. When the ball in tilt
switch runs from one end to the other end because of external force shaking, the tilt
switch will conduct, or it will break.
In this experiment, we will use an LED and a tilt-switch to build a tilt indication light.
When the tilt-switch is tilt, the LED will light up. Otherwise, it will go out.
Experimental Procedures
Step 1: Connect the circuit
9
Step 2: Edit and save the code (see path/Rpi_LcdStartKit /04_tiltSwitch/tiltSwitch.c)
./a.out
Press Enter, if you tilt the switch, the LED will light up. Otherwise, it will go out.
10
11
Lesson 5 Buzzer
Introduction
In this lesson, we will learn how to drive an active buzzer with a PNP transistor to make
sounds.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*Buzzer (Active)
- 1*NPN transistor (8050)
- 1*Resistor (1KΩ)
- Jumper wires
Experimental Principle
As a type of electronic buzzer with integrated structure, buzzers, which use DC power
supply, are widely used in computers, printers, photocopiers, alarms, electronic toys,
automotive electronic equipments, telephones, timers and other electronic products for
voice devices. Buzzers can be categorized as piezoelectric and magnetic buzzers. A
piezoelectric buzzer is mainly composed of multivibrator, piezoelectric buzzer slice,
impedance matcher, resonance chamber, shell, etc. A magnetic buzzer is mainly
composed of oscillator, electromagnetic coil, magnet, vibrating diaphragm, shell, etc.
Buzzers can also be categorized as active and passive buzzers (See the following pictures).
When we place the pins of two buzzers upwards, we can see the one with green circuit
board is a passive buzzer, while the one without circuit board instead of enclosing with
black tape is an active buzzer.
12
The difference between an active buzzer and a passive buzzer is:
The active buzzer has built-in oscillating source, so it will make sounds as long as it is
electrified. While the passive buzzer does not have oscillating source, so it will not tweet if
you use DC signals, instead you must use square waves whose frequencies are between
2K and 5K to drive it. The active buzzer is often more expensive than the passive because
multiple built-in oscillating circuits exist.
In this experiment, we use an active buzzer. When we make the GPIO of Raspberry Pi
output low level (0V) by programming, the transistor will conduct because of current
saturation and the buzzer will make sounds. But when we supply high level to the IO of
Raspberry Pi, the transistor will cut-off and the buzzer will not make sounds.
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram (Pay attention to the
positive and negative poles of the buzzer)
13
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /05_beep/beep.c)
./a.out
Further Exploration
If you have a passive buzzer in hand, you can replace the active buzzer with it. So you
can make it sound “do re mi fa so la si do” through programming as long as you have
some programming foundation and enough patience.
14
Lesson 6 RGB LED
Introduction
RGB LEDs can emit various colors of light. They are manufactured by packaging three
LEDs of red, green, and blue into a transparent or semitransparent plastic shell and lead
out four pins. The three primary colors of red, green, and blue can be mixed to all kinds of
colors by brightness, so you can make RGB LEDs emit light with all kinds of colors by
controlling the circuit.
Components
- 1*Raspberry Pi
- 1*Network cable(or USB wireless network adapter)
- 1*RGB LED
- 3*Resistor (220Ω)
- Several jumper wires
Experimental Principle
The three primary color red, green and blue of a RGB LED can compose various colors by
brightness. We can adjust the brightness of LED with PWM technology. Raspberry Pi has
only one channel hardware PWM output, but we need three channels to control the RGB
LED. As a result, it is difficult to realize with the hardware PWM of Raspberry Pi. Do not
worry! Fortunately the softPwm library simulates PWM (softPwm) for us with software
method. Based on this, we only need to include the header file softPwm.h, then call the
API it provided to easily achieve multi-channel PWM output to control the RGB LED to
display all kinds of colors.
There are two types of package for RGB LEDs. One is patch type, and the other is dual-in-
line type. The difference between them is color resolution. The former has better color
resolution.
RGB LEDs can be categorized into common anode type and common cathode type. In this
experiment, we use common cathode RGB LED.
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram
15
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /06_rgb/rgb.c)
./a.out
Press Enter, you will see the RGB LED light up, and display different colors in turn.
16
Further Exploration
I hope you can modify the parameters of the function ledColorSet( ) by yourself, then
compile and run the program to see the color changes of the RGB LED.
Experimental Summary
You have learnt how to control RGB LEDs with softPwm of the Raspberry Pi through this
lesson. I hope you can continue to explore softPwm application in DC motor speed
regulation.
17
Lesson 7 LED Breathing Light
Introduction
In this lesson, we will gradually increase and decrease the luminance of an LED with PWM
(Pulse-width modulation) technology, which looks like breathing. So we give it a magical
name - Breathing Light.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*LED
- 1*Resistor (220Ω)
- Jumper wires
Experimental Principle
Before we talk about PWM, let’s have a look at the applications of PWM first. PWM has
been successfully applied in motor speed regulation, steering angle control, light intensity
control and signal output. For example, when PWM is applied to a horn, it will make a
sound. After we know about its special functions, let’s find out what PWM really is.
Pulse Width Modulation commonly refers to PWM. Pulse Width Modulation (PWM) is a
digital coding method for analog signal levels. Since a computer cannot output an analog
voltage but digital voltage value 0V or 3.3V, we modulate the duty cycle of square waves
to encode a specific level of analog signal by using a high-resolution counter. PWM signals
are essentially digital signals, for the full amplitude DC power supply is either 3.3V (ON)
or 0V (OFF) at any given time. Voltage or current source is applied to an analog load in
the form of ON or OFF repetitive pulse sequence. When it is on, DC power supply will be
applied to the load; when it is off, DC power supply will be disconnected. If only the
bandwidth is wide enough, any analog value can be encoded by PWM. The output voltage
value is calculated by the on and off time, Vout = (Ton / T)*Vmax.
18
1. The term duty cycle describes the proportion of 'on' time to the regular interval or
'period' of time
2. The term period describes the reciprocal of pulses in one second
3. Voltage amplitude (e.g. 0V-3.3V)
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram
19
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /07_pwmLed/
pwmLed.c)
Step 3: Compile the code
gcc PwmLed.c -lwiringPi
Step 4: Run the program
./a.out
Press Enter and you will see a gradual change of the LED luminance, rendering the effect
of breathing.
Summary
Through this experiment, you should have mastered the technical principle of PWM and
how to program Raspberry Pi with PWM. You can apply this technology to DC motor
speed regulation in the future.
20
Lesson 8 Relay
Introduction
Relays are suitable for driving high power electric equipment, such as lights, electric fans
and air conditioning. We can use a relay to realize low voltage to control high voltage by
connecting it to MCU.
Components
- 1*Raspberry Pi
- 1*Network cable (or USB wireless network adapter)
- 1*Diode (1N4007)
- 1*NPN transistor (8050)
- 1*Resistor (1KΩ)
- 1*Relay
- Several jumper wires
Experimental Principle
Connect the base electrode of the transistor to GPIO0. When we make GPIO0 output low
level (0V) by programming, the transistor will conduct because of current saturation. The
normally open contact of the relay will be closed, while the normally closed contact of the
relay will be broken; when we make it output high level (3.3V), the transistor will be cut
off, and the relay will recover to initial state.
Experimental Procedures
Step 1: Connect the circuit according to the following method
21
Step 2: Edit and save the code with vim (see path/Rpi_LcdStartKit /08_relay/relay.c)
Step 3: Compile the code
gcc relay.c -lwiringPi
Step 4: Run the program
./a.out
Now, press Enter, and you should be able to hear ticking sound. This sound is made by
breaking normally closed contact and closing normally open contact. You can attach a
high voltage device you want to control, for example, a bulb with 220V voltage, to the
output port of relay, so the relay plays a role of automatic switch.
22
23
Lesson 9 Servo
Introduction
Servo is a type of geared motor that can only rotate 180 degrees. It is controlled by
sending electrical pulses from your microcontroller. These pulses tell the servo what
position it should move to.
A servo has three wires, the brown wire is GND, the red one is VCC, and the orange one
is signal line, as shown below.
Components
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*Servo
- Several jumper wires
Experimental Principle
Servo consists of shell, circuit board, non-core motor, gear and location detection. Its
working principle is as follow: Raspberry Pi controller sends PWM signal to servo motor,
24
and then this signal is processed by IC on circuit board to calculate rotation direction to
drive motor, and then this driving power is transferred to swing arm by reduction gear. At
the same time, position detector returns location signal to judge whether set location is
reached or not.
Experimental Procedures
Step 1: Connect the circuit
Servo Raspberry Pi
25
Step 2: Edit and save the code (see path/Rpi_LcdStartKit /09_servo/servo.py)
chmod +x servo.py
python servo.py
Press Enter, you can see the servo motor rotate 180 degrees. And then rotate in opposite
direction.
26
Lesson 10 Thermistor
Introduction
A thermistor is a type of resistor whose resistance varies significantly with temperature.
Components
- 1*Raspberry Pi
- 1*Network cable (or USB wireless network adapter)
- 1*Thermistor
- 1*ADC0832
- 1*Resistor (10K)
- Several jumper wires
Experimental Principle
When the temperature increases, the thermistor resistance decreases; when the
temperature decreases, the thermistor resistance increases. It can detect surrounding
temperature changes in real time.
Analog signals collected by the thermistor are converted to digital signals through
ADC0832. Then we send these digital signals to Raspberry Pi and display them on the
screen.
Experimental Procedures
Step 1: Connect the circuit
27
Step 2: Edit and save the code(see path/Rpi_LcdStartKit /10_thermistor/thermistor.c)
./a.out
Press Enter, if you touch the thermistor, you can see current temperature value displayed
on the screen change accordingly.
28
Lesson 11 Photoresistor
Introduction
A photoresistor or light-dependent resistor (LDR) or photocell is a light-controlled
variable resistor. The resistance of a photoresistor decreases with increasing incident light
intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in
light-sensitive detector circuits, and light- and dark-activated switching circuits.
Components
- 1*Raspberry Pi
- 1*Network cable (or USB wireless network adapter)
- 1*Resistor(10K)
- 1*ADC0832
- 1*Photoresistor
- Several jumper wires
Experimental Principle
With light intensity increasing, the resistance of a photoresistor will decrease. Thus the
output voltage will change.
Analog signals collected by the photoresistor are converted to digital signals through
ADC0832. Then we send these digital signals to Raspberry Pi and display them on the
screen.
Experimental Procedures
Step 1: Connect the circuit
29
Step 2: Edit and save the code (see path/Rpi_LcdStartKit /11_photoresistor/
photoresistor.c)
./a.out
Now, if you change light intensity, you will find the value printed on the screen change
accordingly.
30
Lesson 12 Voltmeter
Introduction
In this lesson, we will use a Raspberry Pi and an ADC0832 to make a voltmeter.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*ADC0832
- 1*Potentiometer
- Jumper wires
Experimental Principle
In this experiment, we use the potentiometer to divide voltage.
Since the Raspberry Pi can only read digital signals, but what the adjusting end of the
potentiometer outputs is an analog signal, so we need to convert the analog signal into
digital signal with an ADC (Analog to Digital Convertor). Then we make this digital output
voltage display on the screen.
Experimental Procedures
Step 1: Connect the circuit as shown in the following diagram
31
Step 2: Edit and save the code (see path/Rpi_LcdStartKit /12_voltmeter/vol.c)
Step 3: Compile the code
gcc vol.c -lwiringPi
Step 4: Run the program
./a.out
Press Enter, if you adjust the potentiometer , you will see the voltage value displayed on
the screen changed.
32
Lesson 13 Controlling LED by Light
Introduction
In this lesson, we will use light intensity to control the luminance of an LED. When the
light gets stronger, the LED will get brighter. When the light gets weaker, the LED will get
darker.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*Photoresistor
- 1*ADC0832
- 1*LED
- 1*Resistor (220Ω)
- 1*Resistor (10KΩ)
-Jumper wires
Experimental Principle
In this experiment, we use PWM to control the luminance of the LED. The greater the
duty cycle is, the brighter the LED is.
The resistance of the photoresistor decreases when the light intensity increases. We use
the photoresistor output as the parameter of PWM after simple calculation by ADC. The
weaker the light intensity is, the greater the PWM duty cycle is, and the brighter the LED
is.
Experimental Procedures
Step 1: Connect the circuit
33
Step 2: Edit and save the code (see path/Rpi_LcdStartKit /13_illumCtrl/illumCtrl.c)
Step 3: Compile the code
gcc illumCtrl.c -lwiringPi -lpthread
Step 4: Run the program
./a.out
Press Enter, if you change the incident light intensity, you will see the luminance of the
LED change accordingly.
34
Lesson 14 Temperature Monitoring and Alarm
Introduction
In this lesson, we will use a thermistor and a buzzer to make a temperature monitoring
and alarm system.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*Thermistor
- 1*Buzzer
- 1*NPN transistor (8050)
- 1*Resistor (1KΩ)
- 1*Resistor (10KΩ)
- 1*ADC0832
- Jumper wires
Experimental Principle
We use an ADC0832 to convert the analog temperature collected by the thermistor to
digital temperature. When the result is greater than 200 degrees Celsius, the buzzer will
alarm.
Experimental Procedures
Step 1: Connect the circuit
35
Step 2: Edit and save the code(see path/Rpi_LcdStartKit/14_tempMonitor/ tempMonitor.c)
Step 3: Compile the code
gcc tempMonitor.c -lwiringPi
Step 4: Run the program
./a.out
Press Enter, if use a lighter to burn the thermistor, you will hear the buzzer alarm.
36
Lesson 15 LCD1602
Introduction
In this lesson, we will learn how to use LCD1602 to display strings.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*LCD1602
- 1*Potentiometer
- Jumper wires
Experimental Principle
LCD1602, also called character type LCD1602, is a dot matrix LCD module that specially
used to display letters, figures, symbols, and so on. It consists of 16X2 dot matrixes, and
each dot matrix is composed of 5X7 or 5X11 character bit. Each character bit can display
a character. There is a dot space between each adjacent character bit. And there is a dot
space between each row too. The dot space functions as a character space or a line space,
and because of this, LCD1602 cannot display graphics very well. It is widely used in
pocket instruments and low power application systems due to its micro power
consumption, small size, rich contents display, ultra-thin and lightness.
Pin 3(Vo): used to adjust the contrast of LCD1602, lowest when connected to positive
power supply, highest when connected to ground (you can connect a 10K potentiometer
to adjust its contrast when using LCD1602);
Pin 4 (RS): A Register Selection pin, select data register when supplied high level (1),
select instruction register when supplied low level (0);
Pin 5 (R/W): A Read/Write signal pin, read signals when supplied high level (1), write
signals when supplied low level (0). Since we only need to write data to LCD1602, we
directly connect this pin to ground;
Pin 6 (E): An Enable pin, when supplied low level, the LCD module will execute relevant
instructions
LCD1602 has two operation modes: 4-bit mode and 8-bit mode. When the IOs of
microprocessor (MCU) is nervous, you can choose 4-bit mode, which only use D4~D7 pins.
After connecting the circuit, you can operate LCD1602 by Raspberry Pi.
Experimental Procedures
Step1: Connect the circuit as shown in the following diagram
38
./Adafruit_CharLCD.py
Press Enter, and you will see two lines of information being displayed on the LCD1602.
The first line display our logo “SUNFOUNDER” and the second line display “Hello
World !:)”. The real effect is shown as follow:
Further Exploration
In this experiment, we drive LCD1602 in 4-bit mode. I hope you can program by
yourselves to drive LCD1602 in 8-bit mode.
Summary
Through this lesson, you have basically mastered the principle and programming
implementation for LCD1602 based on Raspberry Pi. I hope you can create many more
fun works on the basis of Raspberry Pi.
39
Lesson 16 Controlling an LED Based on Web
Introduction
The reason why the Raspberry Pi is so powerful is its networking function and numerous
open source web application support. We can easily use it to realize our own Internet of
Things system. In this lesson, we will control an LED based on web with the Raspberry Pi.
You can use your mobile phone, tablet pc or computer to control the LED too, as long as
it is in the same Local Area Network (LAN) with your Raspberry Pi.
Experimental Conditions
- 1*Raspberry Pi
- 1*Breadboard
- 1*Network cable (or USB wireless network adapter)
- 1*LED
- 1*Resistor (1KΩ)
- Jumper wires
Experimental Principle
WebIOPi is a fully integrated Internet of Things framework for the Raspberry Pi. Webiopi
is a lightweight web server program on Raspberry Pi platform. It provides users a solution
that can access Raspberry Pi remotely. Based on this, you can remotely operate your
Raspberry Pi and control the hardware connected to it by a browser or an APP.
Experimental Procedures
1. Connect the circuit as shown in the following diagram
40
2. Install WebIOPi
wget http://webiopi.googlecode.com/files/WebIOPi-0.5.3.tar.gz
tar xvzf WebIOPi-0.5.3.tar.gz
cd WebIOPi-0.5.3
sudo ./setup.sh
4. Start WebIOPi
sudo python -m webiopi 8000
You can change the port by yourself, the default port is 8000
41
sudo update-rc.d webiopi defaults
Control method:
1) Click outside IN/OUT to switch between the input and output mode of GPIOs
2) In output mode, click inside figures to switch output between high and low. After
connecting the LED to GPIO17, when you click block 11, you will see the states of the
LED changed
3) In input mode, inside figures represent the input states of GPIO
Summary
In this lesson, we have learnt how to build Internet of Things for WebIOPi platform based
on Raspberry Pi and learnt how to control our device remotely based on this platform. I
hope you can learn WebIOPi in depth and use your brain to make many more fun things.
42