• Complete electronics/embedded newbie here, so apologies if I'm just missing something really obvious...

    I'm trying to set up a system where when I connect two Picos, one realises the connection has occurred and sends some data to the other over Serial1.

    The simple way to achieve this I've found is to have both Picos connected via a Serial1 connection (9600/tx:B6/rx:B7), and then connect the 3.3v pin of Pico A to pin B3 of Pico B, and just use setWatch() on Pico B to detect pin B3 being pulled high when they connect.

    This seems to work fine - every time I connect B's B3 to A's 3.3v the watcher triggers exactly once.

    The problem occurs when I try to print text over Serial1 as a result - suddenly Pico B's setWatch() is continually being triggered, as fast as the debounce setting will permit it.

    If I put a counter in and (for example) only output to Serial1 if(counter < 5), I get the expected number of outputs and no more, so obviously outputting to Serial1 isn't simply triggering the setWatch() event handler endlessly - it's doing it exactly once per write to Serial1... and then the event handler writes to Serial1, re-triggering the setWatch() event handler... etc, etc, etc.

    There's no obvious connection I can see between B3 and Serial1 on B6/B7 - can anyone explain what on earth I'm missing?

    Example code follows - B6/B7 each lead to the other pin on another Pico, and I trigger the behaviour by connecting that other Pico's 3.3v pin to B3 on this Pico:

    // Convenience function so I can tell when it's transmitting/listening
    function setTransmitting(transmitting) {
      if(transmitting) {
        digitalWrite(LED1, 1);  // Red = transmitting
        digitalWrite(LED2, 0);  // Green = listening
      }
      else {
        digitalWrite(LED1, 0);
        digitalWrite(LED2, 1);
      }
    }
    
    // Set everything up (so it can be easily called from onInit or the REPL as necessary)
    function setUp() {
    
      USB.setConsole();
    
      /**** Set up inputs ****/
    
      pinMode(B3, 'input_pulldown');  // Listen for connection (other Pico's 3.3v pin pulls B3 high on connection)
    
      /**** Set up outputs ****/
    
      Serial1.setup(9600, { tx:B6, rx:B7 });  // Serial data connection
      setTransmitting(false);                 // Display indicator
    
      /**** Behaviours ****/
    
      setWatch(function(e) {  // On connection, output data
        setTransmitting(true);
    
        Serial1.print('Hello!');
    
        setTransmitting(false);
      }, B3, { repeat: true, edge: 'rising', debounce:500 });  
    }
    
    E.on('init', function() {
      setUp();
    });
    

    TL;DR: Basically it seems as if sending data over Serial1 resets the setWatch() on pin B3, leading to it triggering the event handler even if B3 was already high before the Serial write occurred. Why would writing to Serial1 re-trigger a watch on pin B3?

    P.S. Apologies for the bad title - I only realised after posting that Serial1 was triggering the watch on B3 exactly once per write (and the event handler was then re-sending to Serial1 leading to an infinite loop), and not continually.

  • Which wires do you have connected? Just:

    • 3.3v -> B3
    • B6 -> B7
    • B7 -> B6

    Do you have ground connected between the two Picos as well?

    If you don't, it's possible that could be causing your problems I guess?

  • Which wires do you have connected? Just:

    3.3v -> B3
    B6 -> B7
    B7 -> B6

    Yes - that's all.

    Do you have ground connected between the two Picos as well?

    Ah - I don't, no. I'll give it a try - thanks!

    Edit: That seems to have fixed it - many thanks! Not knowing much about electronics that was seriously confusing the hell out of me - clearly I need to read up a lot more to get my head around more of these basic concepts. Thanks again!

  • No problem! It's pretty common as people tend to forget to mention it, and even circuit diagrams often leave out a connecting wire for it because they assume GND is always connected together :)

    On most things you need GND connected. A voltage isn't really absolute - you (and the microcontrollers) need to measure it relative to something else. It's like me showing you where the top of my head is and asking you to measure my height - it's impossible unless you can also measure from where my feet are.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Printing to Serial1 repeatedly triggers setWatch() on B3 - can anyone explain?

Posted by Avatar for jtq @jtq

Actions