Master - Slave using TX RX (UART)

char data_rcvd;

void setup()
{
pinMode(2, INPUT); // set push button pin as input
pinMode(5, OUTPUT); // set LED pin as output
digitalWrite(5, LOW); // switch off LED pin

Serial.begin(9600); // initialize UART with baud rate of 9600 bps
}

void loop() {
if (Serial.available()) {
char data_rcvd = Serial.read(); // read one byte from serial buffer and save to data_rcvd

if (data_rcvd == '1') digitalWrite(5, HIGH); // switch LED On
if (data_rcvd == '0') digitalWrite(5, LOW); // switch LED Off
}

if (digitalRead(2) == HIGH) Serial.write('0'); // send the char '0' to serial if button is not pressed.
else Serial.write('1'); // send the char '1' to serial if button is pressed.
}

This is the code i used for both master and slave why does whenever i click push button LED doesnt turn on

It looks like your program is continually sending '1’s which turn the LED off and only occasionally sending a ‘0’ so it might be that the blink is too short for you to see it.

Try adding delay(1000); into loop() on one of the programs (the transmitter).

I suggest you start with two quite separate programs, one to send and the other to receive as it will make it a lot easier to debug.

…R

If you have one Ardiuno connected to Serial Monitor, does it send a stream of '0' when you aren't pushing the button and a stream of '1' when you are pushing the button? If not, your button is wired wrong.

i already solve this problem but i encounter another problem

how can i make the led turn off using the push button, if i simulate it i click the push button then led turn on and its turn off on its self without me clicking the push button
this is the code that i used
Transmitter
// Transmitter

int Button1 = 2;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600); // for communication
pinMode(Button1, INPUT); // for read button

}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(Button1) == 0)
{
Serial.write('1');
}

delay(20);
}
Receiver
// Receiver
int LED1 = 5;

char message;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600); // communication
pinMode(LED1, OUTPUT);

}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available())
{
message = Serial.read();
if(message == '1')
{
digitalWrite(LED1, HIGH);
}

}
delay(500);
digitalWrite(LED1, LOW);
}

unknowniiix:
i already solve this problem but i encounter another problem

how can i make the led turn off using the push button,

First, please use the code button </> when posting code

so your code looks like this

it makes it much easier for people to help you

If you want one button click to turn the LED on and another click of the same button to turn the LED off then your code needs a variable to keep track of the state of the the LED - let’s call it LedState and let’s use ‘B’ for bright and ‘D’ for dark

Then when your button code detects the button changing from not-pressed to pressed it should change the variable from B to D or from D to B as appropriate.

At regular intervals (say 5 times per second) send the value of the variable to the other Arduino. If it receives a ‘B’ it will turn the LED on and if it receives a ‘D’ it will turn the LED off

…R

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

unknowniiix, what Robin2 writes is the State Change Detection: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection.

If you want to press the button to turn the led on and press it again to turn it off, then you need the State Change Detection.
If you want the led to turn on as long as the button is pressed, then it can be much simpler.
If you want the led to stay on for 5 seconds after pressing the button, that is also possible.

Anything is possible, can you describe it in a clear way ? With a good description in a logical way, then half the sketch is already written :wink:

The board connections in Tinkercad are okay. You can make it perfect by connecting one leg of the LED to GND. So it will be: pin 5 -> resistors -> led -> GND. It does not matter, but the active component is preferred to be close to GND.