Garbage output at receiver side

Can someone please help me with the receiver side o/p? I am trying to implement a data transmission circuit using lifi technology, the h/w pic of which I have posted here. The problem I am facing is that my transmitter side is working absolutely fine but on the receiver side the command window shows some garbage value instead of the desired char. I am a bit weak with the Arduino coding so can someone please help me out with the same? I have posted the codes as well
transmitter code:
''''''''''
#define LED_PIN A1
#define BUTTON_PIN A0
#define PERIOD 15

char* string = "This is a test transmission";
int string_length;

void setup()
{
pinMode(LED_PIN, OUTPUT);
//pinMode(BUTTON_PIN, INPUT_PULLUP);
string_length = strlen(string);
}

void loop()
{
for(int i = 0; i < string_length; i ++)
{
send_byte(string[i]);
}
delay(1000);
}

void send_byte(char my_byte)
{
digitalWrite(LED_PIN, LOW);
delay(PERIOD);

//transmission of bits
for(int i = 0; i < 8; i++)
{
digitalWrite(LED_PIN, (my_byte&(0x01 << i))!=0 );
delay(PERIOD);
}

digitalWrite(LED_PIN, HIGH);
delay(PERIOD);

}
''''''''
receiver code:
#define LED_PIN 3
#define LDR_PIN A2
#define THRESHOLD 100
#define PERIOD 15

bool previous_state;
bool current_state;

void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
/void loop(){
digitalWrite(LED_PIN, HIGH); // turn the led on
delay(1000); // wait for 1 second
digitalWrite(LED_PIN, LOW); // turn the led off
delay(1000);
}
/

void loop()
{
current_state = get_ldr();
if(!current_state && previous_state)
{
print_byte(get_byte());
}
previous_state = current_state;
}

bool get_ldr()
{
int voltage = analogRead(LDR_PIN);
return voltage > THRESHOLD ? true : false;
}

char get_byte()
{
char ret = 0;
delay(PERIOD*1.5);
for(int i = 0; i < 8; i++)
{
ret = ret | get_ldr() << i;
delay(PERIOD);
}
return ret;
}
void print_byte(char my_byte)
{
char buff[2];
sprintf(buff, "%c", my_byte);
Serial.print(buff);
}
''''''''''''''''

Suggest you connect a wire between the two Arduinos.

Can you please elaborate a bit.

You have to enclose each sketch between back apostrophes to make it look as it should ``` instead of """

The GND/5V from your receiver are not connected. The LDR and the LED will not work properly. Bring GND/5V from the receiver to the right-hand side of the protoboard, remove the white cable that bridges both sides, and move the purple and the red cables to the right hand side.

Please change the orange cable coming from A1 to a different color :pray:

It is a curious way to wire the potentiometer. You should post an electrical diagram. A picture of a hand-drawn one will work

Sorry, that should have said:

Suggest you connect a ground wire between the two Arduinos.

A wire from 0V on one Arduino to 0V on the other Arduino.

I tried the changes you have suggested but the output is not coming. Now it is not showing the garbage value but the output is still not coming.

You are sending the Least Significant Bit (LSB) first. Then, upon receiving, you are assembling the byte backwards (the first bit received, which is the LSB makes its way to the MSB position).

Instead of

for(int i = 0; i < 8; i++)
  {
    ret = ret | get_ldr() << i;
    delay(PERIOD);
  }

Try

for(int i = 0; i < 8; i++)
  {
    ret = ret | get_ldr() << (7-i);
    delay(PERIOD);
  }

There are other issues.

This is an asynchronous communication. You should send a start signal so the receiver knows it's time to be ready to receive a sequence of 8 bits. Currently it waits for the input to change, dismisses the first bit and then starts reading. I cannot see how it gets each byte correctly.

Other issue that you might be having is the speed of the LDR. I am not sure that it will react to a 15ms pulse. Since you did not share an electrical diagram or the LDR specs I cannot say more on this.


this is the circuit diagram now if you can check and tell what is the problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.