Thursday, 14 May 2026

UART is TTL or RS232?

 

UART (Universal Asynchronous Receiver/Transmitter) is technically neither; it is the logic protocol itself, but it almost always operates at TTL levels (\(0V\)-\(3.3V/5V\)) inside microcontrollers. It produces raw data, which can then be transmitted via TTL or converted to RS-232 (\(+12V\) to \(-12V\)) for long-distance industrial communication. [1, 2, 3, 4, 5]
Key Differences:
  • UART (TTL): Used on microcontrollers (Arduino, STM32) and sensors. Idle state is HIGH (\(3.3V/5V\)), and active is LOW (\(0V\)).
  • RS-232: Used on older PCs and industrial equipment. Uses high bipolar voltages (e.g., \(\pm 12V\)) for long distances and noise immunity.
  • Compatibility: You must use a MAX232 level shifter to connect a TTL UART to an RS-232 device.
  • TTL UART (0–Vcc, e.g., 3.3V) is for short-distance communication on a single board.
  • RS-232 (\(\pm 3V\) to \(\pm 15V\)) is for long-distance, industrial, or legacy communication. [1, 2, 3, 4, 5, 6]
Important: Never connect a TTL UART directly to an RS-232 port, as the higher voltages can damage your microcontroller

Arduino to Arduino UART

 https://peppe8o.com/arduino-uart-communication/

 

arduino-uart-wiring-diagram 

 

 

 

Section 1: “uart-trasmitter-code.ino” code

This section contains the code explanation for the transmitter, which performs Serial.write(x). The x is the value that reads from the push button. The push button, when pressed, becomes high, which means that it becomes 1. The baud rate set for the communication is 9600. The LEDs are set here to get the value from the receiver to turn the LED on and off.

#define PB 2
void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(PB, INPUT);
}
void loop()
{ byte x=digitalRead(PB);
  Serial.write(x);
  if(Serial.available())
  {
  byte rec=Serial.read();
  if(rec==1)
  {
    digitalWrite(13,HIGH);
  }    
  else digitalWrite(13,LOW);
  }
}

Section 2: “uart-receiver-code.ino” code

This is the section for the receiving part. Both the receiving and transmitting codes look the same as the communication is happening in half-duplex. So the commands send from the transmitter and received at the receiver by the serial.read() if the value is 1, then the LED turns on; otherwise, it remains off.

#define PB 2
void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(PB, INPUT);
}
void loop()
{ byte x=digitalRead(PB);
  Serial.write(x);
  if(Serial.available())
  {
  byte rec=Serial.read();
  if(rec==1)
  {
    digitalWrite(13,HIGH);
  }    
  else digitalWrite(13,LOW);
  }
}

Simulating the Arduino UART communication

The simulation is made to give the demo, as in the picture it can be seen that the led on the receiver side is on, which happened by pressing the button at the transmitter. Similarly, the second figure, displays that LED is on the transmitter side, in which the button at the receiver is pressed. On pressing the button, the value becomes 1, which means that the transmitted value is 1, while without pressing the button, the value is 0.

arduino-uart-communication-simulation
arduino-uart-communication-simulation-2

What’s Next

Please find more tutorials on Arduino in peppe8o Arduino archives.

Enjoy!

Umar Jamil

For any queries and help for work, please contact me at:
Whatsapp: +92-346-661-7017/ Link
Email: umarjamil0007@gmail.com

peppe8o author image
Umar Jamil (Umar Jamil)

 

 

 

 

 

Monday, 2 June 2025

Arduino Serial Read and Serial Write and Serial Communicaton


 https://docs.arduino.cc/built-in-examples/basics/DigitalReadSerial/

 

https://www.youtube.com/watch?v=g0pSfyXOXj8

Tuesday, 27 May 2025

Sunday, 25 May 2025

Arduino pin 8 adn Pin 8 as Serial Input and Output

 arduino pin 8 as serial input

AI Overview


Learn more
Serial and SoftwareSerial • Wolles Elektronikkiste
The line of code SoftwareSerial mySerial(8, 9); is used in Arduino programming to create a software serial object. It initializes a new serial communication port, but instead of using the standard hardware serial port (0 and 1), it utilizes digital pins 8 and 9 for serial communication. This allows you to use other digital pins for other purposes while still having a serial interface.
Here's a breakdown:

    SoftwareSerial:
    This is a library that enables serial communication on other digital pins of an Arduino board.
    mySerial:
    This is the name you've given to your software serial object. You can use this name to refer to this specific port when sending and receiving data.
    (8, 9):
    These are the two digital pins that will be used for the software serial port. Pin 8 is typically used for the receive (RX) signal, and pin 9 is used for the transmit (TX) signal.

How it works:

    1. Include the library:
    Before using SoftwareSerial, you need to include the SoftwareSerial.h library in your code: #include <SoftwareSerial.h>.

2. Initialize:
The SoftwareSerial mySerial(8, 9); line creates the object and initializes the specified pins for software serial communication.
3. Use the object:
You'll use the mySerial object (e.g., mySerial.begin(9600); to set the baud rate, mySerial.write(...) to send data, and mySerial.available() and mySerial.read() to receive data).
4. Connect:
When using software serial, you'll need to connect the RX pin of the other device (e.g., a Bluetooth module, another Arduino) to pin 8 and the TX pin to pin 9.
5. Baud Rate:
Make sure the baud rate (e.g., 9600, 115200) on both devices is the same for successful communication.
 

Arduino Serial Communication (Jeremy Blum)

 https://www.jeremyblum.com/2011/02/07/arduino-tutorial-6-serial-communication-and-processing/

 

https://forum.arduino.cc/t/how-can-i-control-an-led-via-the-serial-monitor/1052017

 

https://toptechboy.com/arduino-lesson-4-printing-over-the-serial-port/

I2C Arduino

 https://dronebotworkshop.com/i2c-arduino-arduino/

 https://www.circuitbasics.com/how-to-set-up-i2c-communication-for-arduino/

 

 

 

 MASTER

/*
  I2C Master Demo
  i2c-master-demo.ino
  Demonstrate use of I2C bus
  Master sends character and gets reply from Slave
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/

// Include Arduino Wire library for I2C
#include <Wire.h>

// Define Slave I2C Address
#define SLAVE_ADDR 9

// Define Slave answer size
#define ANSWERSIZE 5

void setup() {

  // Initialize I2C communications as Master
  Wire.begin();
 
  // Setup serial monitor
  Serial.begin(9600);
  Serial.println("I2C Master Demonstration");
}

void loop() {
  delay(50);
  Serial.println("Write data to slave");
 
  // Write a charatre to the Slave
  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write(0);
  Wire.endTransmission();
    
  Serial.println("Receive data");
 
  // Read response from Slave
  // Read back 5 characters
  Wire.requestFrom(SLAVE_ADDR,ANSWERSIZE);
 
  // Add characters to string
  String response = "";
  while (Wire.available()) {
      char b = Wire.read();
      response += b;
  }
 
  // Print to Serial Monitor
  Serial.println(response);
}

 SLAVE

/*
  I2C Slave Demo
  i2c-slave-demo.ino
  Demonstrate use of I2C bus
  Slave receives character from Master and responds
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/

// Include Arduino Wire library for I2C
#include <Wire.h>

// Define Slave I2C Address
#define SLAVE_ADDR 9

// Define Slave answer size
#define ANSWERSIZE 5

// Define string with response to Master
String answer = "Hello";

void setup() {

  // Initialize I2C communications as Slave
  Wire.begin(SLAVE_ADDR);
 
  // Function to run when data requested from master
  Wire.onRequest(requestEvent);
 
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
 
  // Setup Serial Monitor
  Serial.begin(9600);
  Serial.println("I2C Slave Demonstration");
}

void receiveEvent() {

  // Read while data received
  while (0 < Wire.available()) {
    byte x = Wire.read();
  }
 
  // Print to Serial Monitor
  Serial.println("Receive event");
}

void requestEvent() {

  // Setup byte variable in the correct size
  byte response[ANSWERSIZE];
 
  // Format answer as array
  for (byte i=0;i<ANSWERSIZE;i++) {
    response[i] = (byte)answer.charAt(i);
  }
 
  // Send response back to Master
  Wire.write(response,sizeof(response));
 
  // Print to Serial Monitor
  Serial.println("Request event");
}

void loop() {

  // Time delay in loop
  delay(50);
}