Controlling A Traffic Light Using Arduino Serial
Feb 23 2021 · 3 min read
How do we dynamically control Arduino outside the code? By giving it the ability to communicate! In this post, we will explore how we can control a traffic light through Arduinio's Serial.
What You Will Need
The prerequisite of this project is that you have already set up your Arduino with a Traffic Lights Module. I’ve provided a tutorial to create your own traffic lights module and setting it up on Arduino. If you don’t have any hardware with you right now, that’s completely okay! You can use this TinkerCAD circuit and follow through!
What’s a Serial?
Arduino provides a way for us to communicate with the device itself. In Arduino’s case, it uses a serial interface; and sends its data bit-by-bit in one single line. Arduino has two dedicated pins for communications, namely pin 0 and 1. Most Arduinos have Tx and Rx labelled in those slots, which stands for Transmit and Receive respectively. As the name suggests, the Tx line will be used to transmit data, while the Rx line for receiving data.
Into the Code!
This code uses the Traffic Lights library we created on the last part, you can use it by following the last project. Here’s the code that we will be using for today:
#include "TrafficLight.h"
String input;
int redPin = 11;
int yellowPin = 10;
int greenPin = 9;
TrafficLight tl(redPin,yellowPin,greenPin);
void setup() {
Serial.begin(115200);
}
void loop() {
while(Serial.available()) {
input = Serial.readString();
if (input.equals("go")) {
tl.Go();
} else if (input.equals("careful")) {
tl.Careful();
} else if (input.equals("stop")) {
tl.Stop();
}
}
}
First things first, we call #include "TrafficLight.h"
to import the traffic lights library.
Then we initialize several variables; the string input
to hold our serial message, redPin
, yellowPin
, and greenPin
to hold our LED pins for the respective colors, and also tl(redPin, yellowPin, greenPin)
that holds the traffic light object.
Next, we initialize the serial baud rate. Serial baud rate dictates the speed at our communication serial operates. In this example, we will be using 115200 baud. You can always choose other baud rates depending on your device’s limitations.
Lastly, in the loop function, we check whenever a there’s an available message in the serial by using
while(Serial.available())
Then, we will read the message passed through the serial using
input = Serial.readString();
Now that we have the input, we can compare the input with our known expected inputs. In our case, we have 3 traffic light states; go
, careful
, and stop
. The traffic light state will change depending on the message that we give the serial.
if (input.equals("go")) {
tl.Go();
} else if (input.equals("careful")) {
tl.Careful();
} else if (input.equals("stop")) {
tl.Stop();
}
Now try it yourself in Arduino’s serial monitor! You can open the serial monitor by clicking on the button top-right of the Arduino IDE window like so:
Once you have opened the serial monitor, don’t forget to set the baud rate according to what we set back in the Arduino! In our case, it’s 115200 baud.
Now try sending data through the serial monitor! Try typing either “go”, “careful”, or “stop” and see how it affects your traffic light!
Summing it All Up
Congratulations on completing the project! Let’s review what we’ve learned today:
- Arduino have in-built serial communication pins labelled Tx and Rx on pins 1 and 0.
- We can use the Arduino’s serial to communicate with the device and control it with our inputs.
Thank you for reading!