LED control with Serial Communication between PC and Arduino

Looking to control an LED using your Arduino and a serial connection? In this tutorial, we’ll show you how to use Tera Term(a popular terminal emulator) installed on a PC to send commands through a DB9 connector and MAX232 chip to turn an LED on and off. This setup is perfect for hobbyists or students exploring serial communication between PC and Arduino. Let’s dive into the code, circuit, and process!

What You’ll Need

  • Arduino board (e.g., Uno)
  • LED with a 220-330 ohm resistor
  • MAX232 IC (for RS-232 to TTL conversion)
  • DB9 female connector
  • Jumper wires
  • A computer with Tera Term installed

The circuit diagram for Arduino based serial communication circuit is shown below.

Here’s how the components are wired together (assuming a basic Arduino Uno setup):

1. LED Connection
  • LED Anode (longer leg): Connected to Arduino digital pin 12 through a 220-330 ohm resistor.
  • LED Cathode (shorter leg): Connected to Arduino GND.
  • Purpose: The resistor limits current to protect the LED, and pin 12 is controlled by the Arduino code (digitalWrite).
2. MAX232 Connections

The MAX232 IC has 16 pins. Key connections include:

  • Pin 1 (C1+) and Pin 3 (C1-): Connect a 1µF capacitor between these pins.
  • Pin 4 (C2+) and Pin 5 (C2-): Connect another 1µF capacitor between these pins.
    • These capacitors help the MAX232 generate ±10V internally from the 5V supply.
  • Pin 2 (V+) and Pin 6 (V-): Leave these as outputs (generated voltages for RS-232).
  • Pin 11 (T1IN): Connect to Arduino TX (pin 1).
  • Pin 14 (R1OUT): Connect to Arduino RX (pin 0).
  • Pin 12 (R1IN): Connect to DB9 pin 2 (RXD).
  • Pin 13 (T1OUT): Connect to DB9 pin 3 (TXD).
  • Pin 15 (GND): Connect to Arduino GND.
  • Pin 16 (VCC): Connect to Arduino 5V.
  • Purpose: The MAX232 translates serial data between the DB9’s RS-232 levels and the Arduino’s TTL levels.
3. DB9 Female Connector
  • Pin 2 (RXD): Receives data from the computer, connects to MAX232 pin 12.
  • Pin 3 (TXD): Transmits data to the computer, connects to MAX232 pin 13.
  • Pin 5 (GND): Connects to the common GND (Arduino and MAX232).
  • Purpose: Interfaces with the computer’s serial port or adapter, carrying commands from Tera Term.
4. Power and Ground
  • Arduino 5V: Supplies power to the MAX232 (pin 16).
  • Common GND: Connects Arduino GND, MAX232 pin 15, LED cathode, and DB9 pin 5.
  • Purpose: Ensures all components share a reference voltage.

How the Circuit Works

  1. Serial Input from Computer:
    • Tera Term sends characters (H, h, L, l) via the DB9 connector.
    • The DB9’s RS-232 signals (e.g., ±12V) enter the MAX232 via pins 12 and 13.
  2. Signal Conversion:
    • The MAX232 converts these to TTL levels (0V/5V) and outputs them on pins 11 (to Arduino TX) and 14 (to Arduino RX).
  3. Arduino Processing:
    • The Arduino’s RX pin (0) receives the data via Serial.read().
    • The code checks if the input is H/h (LED ON) or L/l (LED OFF) and sets pin 12 HIGH or LOW accordingly.
  4. LED Control:
    • Pin 12 drives the LED, turning it on (HIGH, 5V) or off (LOW, 0V).

Simplified Signal Flow

  • Computer (Tera Term) → DB9 → MAX232 (RS-232 to TTL) → Arduino RX → Code → LED (pin 12).

Notes

  • Capacitors: The 1µF capacitors are critical for the MAX232 to function. Polarity matters if they’re electrolytic (positive to C1+, C2+).
  • Arduino Serial Pins: Using pins 0 and 1 means you can’t upload code while the MAX232 is connected—disconnect it during uploads.
  • Safety: Double-check wiring to avoid short circuits or incorrect voltage levels.

Visualizing the Circuit

Imagine a chain: the DB9 feeds into the MAX232, which acts as a translator, passing clean signals to the Arduino, which then flips the LED on or off based on your Tera Term commands. See the video demonstration below.

 

The Arduino Code for LED Control with Serial Communication

Here’s the code to control the LED using serial input


const int ledPin = 12; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's H or h, turn on the LED:
    if (incomingByte == 'H' || incomingByte == 'h') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's L or l, turn off the LED:
    if (incomingByte == 'L' || incomingByte == 'l') {
      digitalWrite(ledPin, LOW);
    }
  }
}
 

Step-by-Step Explanation of the Code

Let’s break down how this Arduino sketch works with Tera Term and the MAX232 setup.

1. Defining Variables
  • const int ledPin = 12;: Sets pin 12 as the LED output pin (adjustable to any digital pin).
  • int incomingByte;: Stores the serial data sent from Tera Term.
2. The setup() Function
  • Serial.begin(9600);: Starts serial communication at 9600 baud, matching Tera Term’s settings.
  • pinMode(ledPin, OUTPUT);: Configures pin 12 as an output for the LED.
3. The loop() Function
  • if (Serial.available() > 0): Checks for incoming serial data from the MAX232.
  • incomingByte = Serial.read();: Reads the byte sent via Tera Term (e.g., H or L).
  • if (incomingByte == 'H' || incomingByte == 'h'): Turns the LED ON if H or h is received.
  • if (incomingByte == 'L' || incomingByte == 'l'): Turns the LED OFF if L or l is received.

Setting Up the Circuit

  1. Connect the LED: Attach the LED’s anode (longer leg) to Arduino pin 12 via a resistor, and the cathode (shorter leg) to GND.
  2. Wire the MAX232:
    • Connect the DB9 female connector to the MAX232 IC (pins 2 and 3 for TX/RX, pin 5 for GND).
    • Link the MAX232’s TTL side to Arduino: TX (MAX232) to RX (Arduino pin 0), RX (MAX232) to TX (Arduino pin 1), and GND to GND.
    • Add capacitors (typically 1µF) to the MAX232 as per its datasheet for voltage conversion.
  3. Power the Circuit: Ensure the Arduino and MAX232 share a common ground and power supply (5V).

Using Tera Term to Control the LED

  1. Install Tera Term: Download and install Tera Term from its official site if you haven’t already.
  2. Connect the Hardware: Plug the DB9 connector into your computer’s serial port (or use a USB-to-serial adapter).
  3. Configure Tera Term:
    • Open Tera Term, select Serial and choose your COM port (e.g., COM1).
    • Go to Setup > Serial port, set the baud rate to 9600, 8 data bits, no parity, 1 stop bit (8N1).
  4. Send Commands:
    • Type H or h and press Enter to turn the LED on.
    • Type L or l and press Enter to turn it off.

Why Use Tera Term and MAX232 with Arduino?

Tera Term provides a lightweight alternative to the Arduino Serial Monitor, while the MAX232 bridges the gap between RS-232 (PC serial) and TTL (Arduino) voltage levels. This setup is ideal for projects requiring external serial control or legacy hardware integration.

Troubleshooting Tips

  • LED not responding? Verify the MAX232 wiring and capacitor values.
  • No serial data? Ensure Tera Term’s baud rate (9600) matches the Arduino code and check your COM port.
  • Connection issues? Test with a multimeter or swap the TX/RX lines.

Expand Your Project

  • Add more commands (e.g., B for blinking) to enhance functionality.
  • Control multiple LEDs by expanding the if conditions.
  • Integrate sensors for automated LED triggers.

Conclusion

Using Tera Term, a MAX232, and Arduino to control an LED is a fun way to explore serial communication. With this guide, you can set up your circuit, upload the code, and start experimenting. Try it out and take your Arduino skills to the next level!

Post a Comment

Previous Post Next Post