Bluetooth Traffic Light

This project is designed to control a traffic light (Red, Yellow, Green LEDs) using Bluetooth communication. The user can send a character (‘R’, ‘Y’, or ‘G’) from a Bluetooth device. When the Arduino receives one of these characters, it lights up the corresponding LED, while ensuring the other two LEDs are turned off.

1. Build the Circuit

../_images/16-Wiring_Bluetooth_traffic_light.png

2. Upload the Code

  1. Open the 02-Bluetooth_traffic_light.ino file under the path of ultimate-sensor-kit\iot_project\bluetooth\02-Bluetooth_traffic_light, or copy this code into Arduino IDE.

  2. After selecting the correct board and port, click the Upload button.

  3. Open the Serial monitor(set baudrate to 9600) to view debug messages.

3. App and Bluetooth module Connection

We can use an app called “Serial Bluetooth Terminal” to send messages from the Bluetooth module to Arduino.

  1. Install Serial Bluetooth Terminal

    Go to Google Play to download and install Serial Bluetooth Terminal .

  2. Connect Bluetooth

    Initially, turn on Bluetooth on your smartphone.

    ../_images/09-app_1_shadow.png

    Navigate to the Bluetooth settings on your smartphone and look for names like JDY-31-SPP.

    ../_images/09-app_2_shadow.png

    After clicking it, agree to the Pair request in the pop-up window. If prompted for a pairing code, please enter “1234”.

    ../_images/09-app_3_shadow.png
  3. Communicate with Bluetooth module

    Open the Serial Bluetooth Terminal. Connect to “JDY-31-SPP”.

    ../_images/00-bluetooth_serial_4_shadow.png
  4. Send command

    Use the Serial Bluetooth Terminal app to send commands to Arduino via Bluetooth. Send R to turn on the red light, Y for yellow, and G for green.

    ../_images/16-R_shadow.png ../_images/16-Y_shadow.png ../_images/16-G_shadow.png

4. Code explanation

  1. Initialization and Bluetooth setup

    // Set up Bluetooth module communication
    #include <SoftwareSerial.h>
    const int bluetoothTx = 3;
    const int bluetoothRx = 4;
    SoftwareSerial bleSerial(bluetoothTx, bluetoothRx);
    

    We begin by including the SoftwareSerial library to help us with Bluetooth communication. The Bluetooth module’s TX and RX pins are then defined and associated with pins 3 and 4 on the Arduino. Finally, we initialize the bleSerial object for Bluetooth communication.

  2. LED Pin Definitions

    // Pin numbers for each LED
    const int rledPin = 10;  //red
    const int yledPin = 11;  //yellow
    const int gledPin = 12;  //green
    

    Here, we’re defining which Arduino pins our LEDs are connected to. The red LED is on pin 10, yellow on 11, and green on 12.

  3. setup() Function

    void setup() {
       pinMode(rledPin, OUTPUT);
       pinMode(yledPin, OUTPUT);
       pinMode(gledPin, OUTPUT);
    
       Serial.begin(9600);
       bleSerial.begin(9600);
    }
    

    In the setup() function, we set the LED pins as OUTPUT. We also start serial communication for both the Bluetooth module and the default serial (connected to the computer) at a baud rate of 9600.

  4. Main loop() for Bluetooth Communication

    void loop() {
       while (bleSerial.available() > 0) {
          character = bleSerial.read();
          Serial.println(character);
    
          if (character == 'R') {
             toggleLights(rledPin);
          } else if (character == 'Y') {
             toggleLights(yledPin);
          } else if (character == 'G') {
             toggleLights(gledPin);
          }
       }
    }
    

    Inside our main loop(), we continuously check if data is available from the Bluetooth module. If we receive data, we read the character and display it in the serial monitor. Depending on the character received (R, Y, or G), we toggle the respective LED using the toggleLights() function.

  5. Toggle Lights Function

    void toggleLights(int targetLight) {
       digitalWrite(rledPin, LOW);
       digitalWrite(yledPin, LOW);
       digitalWrite(gledPin, LOW);
    
       digitalWrite(targetLight, HIGH);
    }
    

    This function, toggleLights(), turns off all the LEDs first. After ensuring they are all off, it turns on the specified target LED. This ensures that only one LED is on at a time.