Lab02 LEDs-Part2
Lab02 LEDs-Part2
Parts required for project 1. In this project, you'll apply the skills you learned in
Project 3 to make a strobe light with adjustable speed settings.
1. Arduino board
2. Breadboard
3. Jumper wires
4. 9 LEDs
5. 50k-ohm potentiometer
6. 9 220-ohm resistors
Parts required for project 2. In this project I’ll introduce a new type of analog sensor
that detects moisture levels. You’ll set up a light and sound alarm system to tell you
when your plant needs watering.
1. Arduino board
2. Jumper wires
3. LED
4. HL-69 hygrometer soil moisture sensor
5. Piezo buzzer
Page 1
PROJECT 1. SECRET KNOCK LOCK
1. How It Works
Turning the potentiometer up or down changes the speed of the flashing lights, creating a
strobe effect. You can use red and blue LEDs for a flashing police light effect (see Figure 1-1). Connect
the LEDs of the same color to the same Arduino pin so they’ll always light together. If you build a
casing to house your LEDs, you’ll have your own mobile strobe unit. You can add up to 10 LEDs; just
update the sketch to include your output pins and the new number of LEDs.
Figure 1-1. Red and blue LEDs mimic the lights of a police car
2. The Build
▪ Step 1. Place your LEDs into the breadboard with the short, negative legs in the GND rail, and
then connect this rail to Arduino GND.
Page 2
▪ Step 2. Insert the resistors into the board, connecting them to the longer, positive legs of the
LEDs. Use jumper wires to connect the two red LEDs together and the two blue LEDs together via
the resistors, as shown in Figure 1-2; this allows the LEDs of the same color to be controlled by a
single pin.
▪ Step 3. Connect the red LEDs to Arduino pin 12 and the blue LEDs to Arduino pin 11.
LEDs ARDUINO
▪ Step 4. Place the potentiometer in the breadboard and connect the center pin to Arduino A0, the
left pin to GND, and the right pin to +5V.
POTENTIOMETER ARDUINO
▪ Step 5. Confirm that your setup matches that of Figure 1-3, and then upload the code in “The
Sketch” on next page.
Page 3
Figure 1-3. Circuit diagram for the disco strobe light
3. The Sketch
The sketch works by setting the analog signal from the potentiometer to the Arduino as an
input and the pins connected to the LEDs as outputs. The Arduino reads the analog input from the
potentiometer and uses this value as the delay value—the amount of time that passes before the
LEDs change state (either on or off). This means that the LEDs are on and off for the duration of the
potentiometer value, so changing this value alters the speed of the flashing. The sketch cycles
through the LEDs to produce a strobe effect.
Page 4
PROJECT 2. ARDUINO MELODY
1. How It Works
You’ll use an HL-69 moisture sensor, readily available online for a few dollars or from some of
the retailers listed in Appendix A. The prongs of the sensor detect the moisture level in the
surrounding soil by passing current through the soil and measuring the resistance. Damp soil
conducts electricity easily, so it provides lower resistance, while dry soil conducts poorly and has a
higher resistance.
The sensor consists of two parts, as shown in Figure 2-1: the actual prong sensor (a) and the
controller (b). The two pins on the sensor need to connect to the two separate pins on the controller
(connecting wires are usually supplied). The other side of the controller has four pins, three of which
connect to the Arduino.
Figure 2-1. The HL-69 moisture sensor prong (a) and controller (b)
The four pins are, from left to right, AO (analog out), DO (digital out), GND, and VCC (see Figure
2-2). You can read the values from the controller through the IDE when it’s connected to your
computer.
This project doesn’t use a breadboard, so the connections are all made directly to the Arduino.
Figure 2-2. The pins are labeled on the underside of the module
Page 5
Lower readings indicate that more moisture is being detected, and higher readings indicate
dryness. If your reading is above 900, your plant is seriously thirsty. If your plant gets too thirsty, the
LED will light and the piezo buzzer will sound. Piezos are inexpensive buzzers and are explained more
in orther Project.
2. The Build
▪ Step 1. Connect the sensor’s two pins to the + and – pins on the controller using the provided
connecting wires, as shown in Figure 2-3.
▪ Step 2. Connect the three prongs from the controller to +5V, GND, and Arduino A0 directly on
the Arduino, as shown in the following table. The DO pin is not used.
VCC +5V
GND GND
A0 A0
DO Not user
▪ Step 3. Connect an LED directly to the Arduino with the shorter, negative leg in GND and the
longer, positive leg in Arduino pin 13, as shown in Figure 2-4.
Page 6
LED ARDUINO
VCC +5V
GND GND
▪ Step 4. Connect the piezo buzzer’s black wire to GND and its red wire to Arduino pin 11.
LED ARDUINO
VCC +5V
GND GND
▪ Step 6. Check that your setup matches that of Figure 2-5, and then upload the code in “The
Sketch” below.
▪ Step 7. Connect the Arduino to your computer using the USB cable. Open the Serial Monitor in
your IDE to see the values from the sensor—this will also help you to calibrate your plant monitor.
The IDE will display the value of the sensor’s reading. My value was 1000 with the sensor dry and
not inserted in the soil, so I know this is the highest, and driest, value. To calibrate this value, turn
the potentiometer on the controller clockwise to increase the resistance and counterclockwise to
decrease it (see Figure 2-6). When the sensor is inserted into moist soil, the value will drop to about
400. As the soil dries out, the sensor value rises; when it reaches 900, the LED will light and the
buzzer will sound.
Page 7
Figure 2-6. Turn the potentiometer to calibrate your plant monitor
3. The Sketch
The sketch first defines Arduino pin A0 so that it reads the moisture sensor value. It then
defines Arduino pin 11 as output for the buzzer, and pin 13 as output for the LED. Use the
Serial.Println() function to send the reading from the sensor to the IDE, in order to see the value on
the screen. Change the value in the line.
if(analogRead(0) > 900){
depending on the reading from the sensor when it is dry (here it’s 900). When the soil is moist,
this value will be below 900, so the LED and buzzer will remain off. When the value rises above 900,
it means the soil is drying out, and the buzzer and LED will alert you to water your plant.
Page 8