Assignment 9: Wired Communication

Serial Communication using UART




Arduino Serial Communication using UART

This week's assignment was to demonstrate two microcontrollers communicating over wired or radio connection. I wanted to try serial communication between an Arduino and PC to turn an LED on and OFF via the serial monitor. This is because controlling LEDs is an important component of my final project. So, I wanted to learn how to control them both via the computer and also via a remote server (assignment 10). Below is an outline of my process:

  1. First, I wanted to learn how to work with Arduino Uno's UART Pins Tx/Rx to control an LED. This is a simple project that requires an LED connection and a sketch. The circuit consists of connecting the positive end to digital pin 13 and the negative end to GND.
  2. Image of simple LED circuit

    Press fab  sketch

  3. Next, I wrote a simple program using the digitalWrite() and serial.println() functions that would allow me to send 0 or 1 from the serial monitor to turn the LED OFF or ON. See code below:
  4. Sample code to communicate with Arduino from computer to turn LED ON/OFF (copy and paste into Arduino IDE):

      int led = 13;
    
    int  value = 0;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      pinMode(led, OUTPUT);
      
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
     
      if(Serial.available()>0){
        value = Serial.read();
        delay(5); 
      }
      if(value == '1'){
        digitalWrite(led, HIGH);
        Serial.println("LED is ON");
      }
       if(value == '0'){
        digitalWrite(led, LOW);
        Serial.println("LED is OFF");
    }
    } 

  5. After uploading the code to the Arduino, I opened a new serial monitor window. Sending 0 to the Arduino turns the LED OFF and prints the message "LED is OFF" while sending 1 turns the LED ON and prints the message "LED is ON".
  6. Demonstration of controlling LED via computer:

    Screen recording of sending signals from serial monitor to Arduino:


    The relevant files are available below for download: