-1

How do i code to transmit a sequence of bit using arduino uno?My project is about a transmitter and a receiver.Arduino as the transmitter and an application as the receiver.Light sensor will detect the bits sent.How do i send a sequence of bit for example 00101010.What kind of function should i use? i am a beginner for arduino. thank you.

13
  • Transmit how? Using morse-code with the led?
    – Gerben
    Commented May 1, 2018 at 13:56
  • 1
    Optical transmission is quite a broad field. What are your requirements regarding speed, error-correction? How do you want to receive and decode the data? You might be better of using an IR library or a different medium (WiFi, Bluetooth, ..) Commented May 1, 2018 at 13:58
  • transmit using morse-code but transmit binary. That will sent 1's and 0's that is 8 bit. the arduino uno act as a transmitter and an application will be receiver and accept the sequence of bit.
    – Nyssa
    Commented May 1, 2018 at 14:03
  • transmit using morse-code but transmit binary say again. If you have "A" would you like this transmitted as "short-long" or "01000001" (ASCII value). Commented May 1, 2018 at 14:09
  • Serial.print("hello"); is transmit binary (ASCII). So your sketch is already doing that. Commented May 1, 2018 at 14:35

1 Answer 1

1

Bit-wise sending of a chunk of data is really just:

  • go through each byte of the data
  • for each byte (= 8-bits), put each bit on the LED and wait

This also has some substantial drawbacks:

  • no error detection or correction. If you get interference on your light sensor / camera due to the environment, you will read wrong data. For this you would need to code the data with some coding scheme or append a checksum
  • outputting on the built-in LED on the Arduino Uno / Nano is a bad idea sine it's directly next to the power, RX and TX LED
  • outputting in the visible spectrum is asking for interference. That's why we have InfraRed transmissions.
  • receiver and transmitter might out of sync; that means that the receiver does not sample the output of the transmitter at the correct time -- you should use a preamble, oversampling and averaging at the receiver to overcome this

That said, you might be better off using other proven methods such as IR, 2.4GHz transceivers, et cetera, from my point of view. Other microcontrollers (example) have even built-in peripherals to do IR transmission according to some protocol.

On the Arduino Uno/Nano you could also use the IRemote library to send and receive data using a cheap IR LED and a IR receiver, many of which are included in starter kits (and are also otherwise cheap).

The code below simply takes a stream of bytes and outputs them bit-wise on the LED. Nothing else done. Also, digitalWrite is slow and should be replaced by a faster variant.

#include <Arduino.h>

/* LED to output from */
const int LEDTrans = 13;
/* Length of a symbol */
const int timeWait = 500;

const char dataToSend[] = "Hello World";

/* Transmits data bitwise */
void transmitData(const char* data, size_t length) {
    //go through each byte of the data
    for(size_t i = 0; i < length; i++) {
        uint8_t currentByte = (uint8_t) data[i];

        //Go through each of the 8 bits in the byte
        //Goes from LSB to MSB (right-to-left)
        for(int j = 0; j < 8; j++) {
            //uses Arduino.h macro "bitRead"
            bool currentBit = bitRead(currentByte, j);

            //Set the LED on/off
            digitalWrite(LEDTrans, currentBit);
            //Wait for the symbol time
            delay(timeWait);
        }
    }
}

void setup() {
    pinMode(LEDTrans, OUTPUT);
}

void loop() {
    transmitData(dataToSend, strlen(dataToSend));
    //turn LED OFF again
    digitalWrite(LEDTrans, 0);
    delay(2000);
}
2
  • >transmit using morse-code but transmit binary say again. If you have "A" would you like this transmitted as "short-long" or "01000001" (ASCII value)..... i'm sorry i didn't notice your question Maximilian. i want to transmit in the form of 01000001.
    – Nyssa
    Commented May 18, 2018 at 14:31
  • can i have your email?i need to ask a lot..
    – Nyssa
    Commented May 18, 2018 at 14:51

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