Pyserial + Arduino - launching py program restart arduino code

Hi there !

a made a code on arduino that takes two infos :

  • a serial signal from a Python code

  • and a push button

the idea is to trigger the same thing either its from a PC or a push button (or multiple ones)

technically, both are working. However when i launch my python program,

let's say the LED is ON, it sets immediately OFF. but i need only one program (not two files , each one for ON and OFF commands)

the idea behind this is to have a desktop icon that makes the same job as the push button. to switch ON or OFF a lamp from both commands for example.

here's the python code :

import serial
import time

node = serial.Serial(port='COM3',baudrate=9600, timeout=.1)
time.sleep(2)
node.write(b'H')
print(node.readline())

and the Arduino code :

#define led 10
int x;
bool toggle;
int _button = 8;

void setup() 
{  
  pinMode(led, OUTPUT);
  pinMode(_button, INPUT);
  Serial.begin(9600);
  Serial.setTimeout(1);
}
void loop() 
{
  byte probe = digitalRead(_button);  

  if(probe == LOW)
  {
      toggle = !toggle;  
      Serial.println("The button line have been activated");  
  }   
  if(Serial.available() > 0){
    x=Serial.read();
    Serial.print("Python, did you say.. ");Serial.print(x);Serial.print(" ? ");
    if(x == 'H')
    {
      toggle = !toggle;
    }    
  }  
    if(toggle == HIGH)
    {
      digitalWrite(led, HIGH);
    }
    else if(toggle == LOW)
    {
      digitalWrite(led, LOW);      
    } 
  delay(250);
}

i wonder what could create that reset command

When Python opens the serial port a DTR signal is sent which resets the Arduino.

Unless you have external pull up or pull down resistor on your input pin you can get arbitrary results from the floating input pin. You could use INPUT_PULLUP to use the internal pull up.

i already got a pullup resistor (and i tried PULLUP mode as you adviced :slight_smile: ).

what do you mean by arbitrary result ?

there is a way to deactivate the DTR signal

in python3 version, you add dsrdtr = True (not False, i've been confused first lol)

node = serial.Serial(port='COM3',baudrate=9600, dsrdtr = True, timeout=.1)

and it totally works

i can turn ON or OFF my line with an external button and my python program !

thank you so much for telling me about the dtr matter ! i wasn't aware of this :smiling_face:

When the input is floating it can be read as high or low depending on the environment around the pin.

I used to do a demo where I had a push button at the end of a couple of feet of wire connected to an input pin set as INPUT mode and ground. I turned on an LED when the pin was low. If I left the button unpressed the LED would flicker as I waved the wires around - the change in electrical noise and my hand capacitance was enough to change the reading. When I changed to INPUT_PULLUP it behaved as expected.

ah yes i see what you mean

i wonder if i testified the same phenomenon just yesterday.

i accidentally touched with my hand or my elbow to a (painted) metal radiator just beside of my desk, and that made a 12v powered relay struggeling on my table

lucky the relay didn't burn !

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