I am working on communicating 4 switch states across multiple Arduinos using only one pin. It must be as fast as conceivably possible.

Currently I have a master Arduino reading all 4 switches (multiplexing) and encoding them as 4-bit values: 0001 = 0, 0010 = 1, 0011 = 2 and so on up to 1111 = 15. I am thinking of assigning the low and high pulse duration/width, (+1) to produce a PWM square wave. So 1+0=1, 1+1=2, 1+2=3 ... 1+15=16. The high width (or low width) will hold the muxed value (+1.) Each slave would read this muxed value and demux it to get the state of all 4 switches.

Now to my concern: Is this approach safe and reliable? Or should I triple up the values: 1=3, 2=6, 3=9 ... to build in some tolerance and prevent some time misread due to some miss-interpolations? My switches control motors and a misread could have some unpleasant consequences.

I am using the Arduino's internal crystal, nothing external.

As a side note: I am actuality using Seeeduinos XIAO and not an Arduino. I started working on the timer based PWM writing and reading aspect, but it turns out to be quite complicated as there is not a lot of exaples to be found.

  • 2
    Please define "reliable" in aspects of hardware and software. -- HW: How long are the wires between the reading Arduino and the motor Arduinos? How much interferences do you expect? -- SW: You could use multiple readings in the receivers and a majority decision. -- Radio control servos get their position command by a pulse width, 1 to 2 ms every 20ms. The resolution is 1% or better. Expensive models in delicate conditions (think about high speed airplanes) are controlled by this. So PWM is a way with some reliability. Commented Aug 20, 2021 at 6:25
  • 2
    Why not just use Serial? You can use some pretty fast baud rates reliably and you only need 1 wire for transmission only.
    – Majenko
    Commented Aug 20, 2021 at 8:55
  • 1
    The Arduino PWM output goes from 0 to 255, so you could multiply by 15 for a maximum value of 240 to give a greater margin of error. Though the serial option is better. You could send data packets with CRC/ECC for greater reliability.
    – tim
    Commented Aug 20, 2021 at 11:23
  • Actually, you could do both PWM and serial. The receivers could compare the two and only act if they agree whilst flagging discrepancies.
    – tim
    Commented Aug 20, 2021 at 11:35
  • 1
    @Majenko Access the UART registers directly ... I did not know you can do that with serial. Having to learn about timers was taxing as is; not sure if I have it in me to learn about UART registers.
    – MeSo2
    Commented Aug 20, 2021 at 15:16


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.