Communication between two embedded devices using UART port with code
Communication between two embedded devices (Arduino Kit) using UART port with code
Components Required:
1) Arduino Uno R3 (x2)
2) Push Button
3) Resistor 220 Ohm
4) Breadboard Mini (x2)
5) Led
Code for Circuit:
Code for Arduino Uno 3 - connected with push button
void setup() {
//set push button pin as input
pinMode(6, INPUT_PULLUP);
//initialize UART with baud rate of 9600 bps
Serial.begin(9600);
}
void loop()
{
{
if (digitalRead(6) == HIGH)
{
Serial.write('0');
Serial.println("HIGH");
}
else
{
Serial.write('1');
Serial.println("LOW");
}
}
delay(100);
}
Code for Arduino Uno 3 - connected with led
void setup() {
//set LED pin as output
pinMode(8, OUTPUT);
//initialize UART with baud rate of 9600 bps
Serial.begin(9600);
}
void loop() {
if(Serial.available())
{
//read one byte from serial buffer and save to data_rcvd
char data_received = Serial.read();
//if push button pressed switch LED On
if(data_received == '1') digitalWrite(8, HIGH);
//if push button is not pressed switch LED Off
if(data_received == '0') digitalWrite(8, LOW);
}
}
Comments
Post a Comment