Skip to content
Advertisement

How can I send and receive data between two PiPicos using LEDs?

How can I code two Pi Picos to send/receive data accurately to 10 milliseconds a bit using LED/Photodiode pairs? I am trying to send data from one PiPico to another wirelessly. I am on a bit of a time crunch and have unsuccessfully tried getting my photodiode circuit to output the correct voltage swings to use pico’s UART pins. There must be a way to to this more simply.

Right now I am using utime.sleep(), and when I blink my LEDs faster than 0.1 seconds a bit, I get an inaccurate byte or two quite often. I am very new to microcontrollers but dont think I should be loosing so much information at 0.01 seconds. I thought it might be that the Pi pico clocks weren’t synced at first, but I’d think 0.01s is slow for the clocks to be so out of sync.

Here is my code: Pico Sending Bits through LEDs:

    for i in range (0, len(finalBitString)): 
            if finalBitString[i]=="1":
                led(1)  
            elif finalBitString[i]=="0":
                led(0)
            utime.sleep(.01)

Receiving Pi Pico:

    while True:
           utime.sleep(.005)   #sample diode every 0.01 seconds
           value = SaqDiode.read_u16()*3.3 / 65536
           if (bitCutoff <= value):      #read saq diode and determine 1 or 0
              bit=1  
           elif (value <= bitCutoff):
              bit=0
           utime.sleep(.005)

Advertisement

Answer

There are several problems with your approach:

  1. You only perform a single sample during each period, which means any slightest lack of sync between the two devices’ clocks will corrupt the transmission.
  2. It looks like your diode is hooked to an ADC, but you don’t take in to account the time it takes to read the ADC.
  3. You have no means to synchronize transmitter and receiver, I assume you rely on your ability to hit the reset button on both at the same time. That is not Accurate enough!
  4. You are using Python, which is not great for time sensitive projects like this one.

Basically, this approach is too simplistic, and relies on too many conditions being “just right”, which they seldom are.

To solve the synchronization issue, you should use a “Self-clocking signal“.

One example that should be easy to implement in your case would be the “Manchester code“.

You should check the spec on PiPico ADC and your diode, and increase the sampling rate as much as possible.

You should than take multiple samples and monitor when transition between a series of on samples and off samples occurs.

It would be even better to get rid of the ADC all together, and rig the diode signal to be either low or high so you can put it on a digital pin that supports interrupts.

Than, instead of a loop polling the diode, you can have your code fired by the system on state transition, which will be a lot more accurate.

Here is a tutorial on how to do the interrupt part: https://microcontrollerslab.com/pir-motion-sensor-raspberry-pi-pico-external-interrupts-tutorial/

Unfortunately, without knowing what kind of photo diode you are using, it is impossible to say how to wire it to work with a digital instead of analog pin.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement