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.
1 Answer
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);
}
-
>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.– NyssaCommented May 18, 2018 at 14:31
-
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).Serial.print("hello");
is transmit binary (ASCII). So your sketch is already doing that.