1

How do i code to send a character 'A' to the arduino through a blinking led? i am doing a project about data transmission. i really need help&some guide. Material used are arduino uno r3,white led and a resistor.

MY OUPUT IN SERIAL MONITOR

Insert letter:

01000001 65 - Character A

00001010 10 - when i send/enter

CODE

 int LEDTrans = 13;
int timeWaitOn = 1000; //milisec (1 sec)
int timeWaitOff = 1000; // milisec (1 sec)
byte byteRead;

void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);


 Serial.print("Insert letter:");
 Serial.println(" ");
}

void loop() {

digitalWrite(LEDTrans,HIGH); //turn the led on
delay(timeWaitOn); //delay in millisec (1 sec)
digitalWrite(LEDTrans,LOW); //turn the led off
delay(timeWaitOff); //wait a sec


   /*  check if data has been sent from the computer: */
  if (Serial.available()) {


     byteRead = Serial.read();
    // will print the binary representation of 'byteRead' as 8 characters of '1's and '0's, MSB first.
for ( uint8_t bitMask = 128; bitMask != 0; bitMask = bitMask >> 1 ) {

      if ( byteRead & bitMask ) {

    Serial.write('1');
  } else {
    Serial.write('0');
  }  
}

Serial.write("    ");
Serial.println(byteRead);    //send back the actual ASCII code
}


}
  • you forgot to ask a question – jsotola May 18 '18 at 4:31
  • There is no code intended to send something over a LED. Try googling something like Arduino light data transmission and similar terms. There are libraries for that – chrisl May 18 '18 at 5:20
  • Possible duplicate of transmit a sequence of bit using arduino uno – Maximilian Gerhardt May 18 '18 at 9:55
  • Huh, I'm confused. You asked this exact question here arduino.stackexchange.com/questions/52352/… and the given answer is also the exact functional equivalent, except that the data is coming from the serial monitor.. – Maximilian Gerhardt May 18 '18 at 9:55
  • i'm learning a lot from this website. im a beginner and i want to learn a lot of things. your explanation and help was really helpful :) im really grateful for that. thank you so much. – Nyssa May 18 '18 at 14:12
0

Instead of printing '0' or '1' set the LED pin off or on, like this:

const int LEDTrans = 13;
const int timeWaitOn = 1000;  // millisec (1 sec)
const int timeWaitOff = 1000; // millisec (1 sec)

void setup() {
  // Turn the Serial Protocol ON
  Serial.begin(115200);
  Serial.println("Send letter:");
  pinMode (LEDTrans, OUTPUT);
}

void loop() {
  /*  check if data has been sent from the computer: */
  if (Serial.available()) {
    byte byteRead = Serial.read();
    // will print the binary representation of 'byteRead' as 8 characters of '1's and '0's, MSB first.
    for ( uint8_t bitMask = 128; bitMask != 0; bitMask >>= 1 ) {
      if ( byteRead & bitMask ) {
        digitalWrite(LEDTrans, HIGH); //turn the led on
        delay(timeWaitOn); //delay in millisec (1 sec)
        digitalWrite(LEDTrans, LOW); //turn the led off
      } else {
        delay(timeWaitOff); //wait a sec
      } // end of if
    } // end of for loop
  } // end of if something available
} // end of loop

how do i count the flashes? is it based on the binary 01000001?

Here's the problem ... according to what your question seems to ask for, you are wanting an LED to flash for a 1 and be off for a zero. So the results (which my code provides) would look like this:

Example 1

So, you get two flashes, the first one second after it starts, then there is a gap of 5 seconds, and then another flash. That would be 01000001.

However, how do you know when to start counting flashes? It might be:

Example 2

That would be 00000001.

Or even:

Example 3

That would be 10000010.

You can see, can't you, that you can't make any meaningful deduction at the receiving end about what the character is?


So what asynchronous serial (normal serial on an Arduino) does is have a Start Bit which signals when to start counting, like this:

Serial with start bit

Normally the serial port is high (continuous 1s) and then when a 0 arrives that means "start counting". So, you wait 1½ bits of time and then sample at the known baud rate. In your example, where you want 1 second per bit, the baud rate would be 1 (one bit per second). Thus, you sample every one second. Normally baud rates are somewhat faster than that, like 9600 or 115200 samples per second.

Also there is a Stop Bit which is always a 1. This guarantees that there will be at least a single 1 at the end of each byte, so that we can tell when the next start bit arrives.


There are other ways of doing this, for example a short pulse might represent a zero, and a longer pulse represent a one. In that case, any pulse at all could represent the start of a byte, and then you just measure how many short pulses, and how many long pulses, you get (for some definition of "short" and "long").

| improve this answer | |
  • thank you soo much the blinking happened. but i am a bit confused. I count the on and off for the led to see the character of 'A' in binary that is 01000001. but it didn't happened. – Nyssa May 18 '18 at 6:55
  • Really? I changed the baud rate to 115200, I hope you noticed. If I send "A" then I get the LED flashing after 1 second and after 7 seconds, which is right: 01000001. You expect two flashes, one a second in, and one seven seconds in. The rest of the time the LED is off (that is a zero). – Nick Gammon May 19 '18 at 0:45
  • Oh. I noticed that,but i tried to change to 9600.Maybe that's why. Do i really must use the baud rate 115200 to achieve 01000001 ?What type of library should I import in this coding?Where can i get the library?This arduino acts as a transmitter to send the character 'A' and the receiver is an application. Do you have any suggestion how can i synchronize both of them?Can i have your email. i have a lot to ask. – Nyssa May 19 '18 at 8:34
  • The baud rate is only for you to send a character to the sketch from the serial monitor. The rate at which the LED blinks is set in your code, currently one bit per second. There is no library you need. Why do you think you want one? If you have more questions please post them here. – Nick Gammon May 19 '18 at 21:38
  • I thought I need one. I went through other projects they import libraries. But I currently don't understand what you mean by >> flashing after 1 second and after 7 seconds, which is right: 01000001. You expect two flashes, one a second in, and one seven seconds in. The rest of the time the LED is off (that is a zero). – Nyssa May 19 '18 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.