Assignment 06 IOT
Assignment 06 IOT
6 (Group B)
Title: Create a program that illuminates the green LED if the counter is less than
100, illuminates the yellow LED if the counter is between 101 and 200 and illuminates the red LED if the
counter is greater than 200
Outcome: Connectivity, configuration and control of LED using Arduino circuit under
different conditions.
Hardware Requirement: Arduino, LED, 220 ohm resistor etc.
Software Requirement: Arduino IDE
Theory:
The problem statement is like Arduino traffic light, a fun little project that you can build
in under an hour. Here's how to build your own using an Arduino, and how to change
the circuit for an advanced variation.
What You Need to Build an Arduino Traffic Light Controller
Apart from the basic Arduino, you'll need:
● 1 x 10k-ohm resistor
● 1 x pushbutton switch
● 6 x 220-ohm resistors
● A breadboard
● Connecting wires
● Red, yellow and green
LEDs Arduino Traffic Light: The
Basics
Let's start small. A basic, single traffic light is a good place to start. Here's the circuit:
Connect the anode (long leg) of each LED to digital pins eight, nine, and ten (via a 220-
ohm resistor). Connect the cathodes (short leg) to the Arduino's ground.
Code for the Arduino Traffic Light Start by defining variables so that you can address the lights
by name rather than a number. Start a new Arduino project, and begin with these lines:
Next, let’s add the setup function, where you'll configure the red, yellow and green LEDs
to be outputs. Since you have created variables to represent the pin numbers, you can
now refer to the pins by name instead:
pinMode function configures the Arduino to use a given pin as an output. You have
to do this for your LEDs to work at all. Now for the actual logic of the traffic light. Here's
the code you need. Add this below your variable definitions and setup function:
Upload this code to your Arduino, and run (make sure to select the correct board and
port from the Tools > Board and Tools > Port menus). You should have a working traffic
light that changes every 15 seconds, like this (sped up):
Let's break down this code. The changeLights function performs all the hard work. This
rotates the traffic light through yellow and red, then back to green. As this gets called
inside the loop function, the Arduino will run this code forever, with a 15-second pause
every time.
The changeLights function consists of four distinct steps:
● Green on, yellow off
● Yellow off, red on
● Yellow on, red on
● Green on, red off, yellow off
These four steps replicate the process used in real traffic lights. For each step, the code is
very similar. The appropriate LED gets turned on or off using digitalWrite. This is an
Arduino function used to set output pins to HIGH (for on), or LOW (for off).
After enabling or disabling the required LEDs, the delay makes the Arduino wait for a
given amount of time. Three seconds in this case.
Going Deeper: Arduino Pedestrian Crossing
Now that you know the basics, let's improve it. Add in a pushbutton for pedestrians to
change the light whenever they like:
Notice how the traffic light is exactly the same as the previous example. Connect the
button to digital pin 12. You'll notice that the switch has a high-impedance 10k-ohm
resistor attached to it, and you may be wondering why. This is a pull-down resistor.
A switch either lets the current flow or doesn't. This seems simple enough, but in a logic
circuit, the current should be always flowing in either a high or low state (remember, 1
or 0, HIGH or LOW). You might assume that a pushbutton switch that isn't actually
pressed would be in a LOW state, but in fact, it's said to be 'floating', because no current
gets drawn at all.
In this floating state, it's possible that a false reading will occur as it fluctuates with
electrical interference. In other words, a floating switch is giving neither a reliable HIGH
nor LOW reading. A pull-down resistor keeps a small amount of current flowing when
the switch gets closed, thereby ensuring an accurate low state reading.
In other logic circuits, you may find a pull-up resistor instead, and this works on the
same principle, but in reverse, making sure that particular logic gate defaults to high.
Now, in the loop part of the code, instead of changing the lights every 15 seconds, you're
going to read the state of the pushbutton switch instead, and only change the lights
when it's activated.
Code for the Arduino Pedestrian Crossing
Start by adding a new variable to store your button pin:
Now, in the setup function, add a new line to declare the switch as an input. Add a line to
set the traffic lights to the green stage. Without this initial setting, they would off until
the first time changeLights runs.
Once again, all the work is carried out in the changeLights function. Rather than going
red > red & yellow > green, this code will alternate the traffic lights. When one is on
green, the other is on red. Here's the code:
------------------------------------------------------------------------------------------------------------------------
Conclusion: -
_____________________________________________________________________________________
______________________________
_____________________________________________________________________________________
______________________________
_____________________________________________________________________________________
______________________________
_____________________________________________________________________________________
______________________________
_____________________________________________________________________________________
______________________________
-------------------------------------------------------------------------------------------------------------------------