Question

I got a problem. I recently bought an Arduino Uno board. I tried to make something funny like controlling an input from my computer. I used Python with pySerial and the program is the following:

arduino = serial.Serial(portaCOM, 9600, timeout = 1)
... in loop ->
arduino.write(value)


  def sliderUpdate(self, event):
        pos = self.slider.GetValue()
        arduino.write(pos)
        time.sleep(.1)
        print arduino.readline()

try:
    arduino = serial.Serial(portaCOM, 9600, timeout = 1)
except:
    print "Errore di connessione alla porta seriale"

The write value should send the value to my board though USB. The program loaded on board is:

 const int ledPin = 11;
 byte brightness;

 void setup(){
     Serial.begin(9600);
     pinMode(ledPin, OUTPUT);
 }

 void loop(){
     while(Serial.available()){
         brightness = Serial.read();
         Serial.print(brightness);
         analogWrite(ledPin, brightness); //LED doesn't refresh the brightness
         delay(10);
     }
 }

My LED is working properly. I tried with the Fade example provided by Arduino and it's working..

I checked if the program is sending properly the data. Yes, it is. It returns the same thing I sent before.

It should retrieve the value sent and set analaogWriter(pin, VALUE), but something is wrong or not working.

How can I fix this problem?

Solution

The Arduino code

const int ledPin = 11;
byte valoreLed;

void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop(){
    while(Serial.available()){
        valoreLed = Serial.read();
        analogWrite(ledPin, valoreLed);
        delay(20);
    }
}

Python script code:

pos = self.slider.GetValue()
arduino.write(chr(pos))

Thank you to everybody!! :)

Was it helpful?

Solution

  1. First of all, make sure your LED is properly connected. Anode (longer pin) to PWM 11 port and cathode (shorter pin) to ground, also you may need to add a resistor between cathode and ground depending on LED.
  2. Make sure you're writing to the right port from python (that FTDI cable is associated with in your OS).
  3. If you're not using FTDI cable with USB connector, make sure that all of the pins are connected to the right inputs.
  4. What is the value of value in your example? Try arduino.write(chr(0xFF)), does LED stay lit?

OTHER TIPS

I suspect that arduino.readline() waits for a newline, and you Arduino code never sends a newline. Hence the Python code blocks indefinitely after sending the first value.

I see in your comments that the LED works with the first character, but you don't see a change with the different characters. Definitely make sure you're sending ASCII characters that are significantly different from the previous characters, e.g. send an ASCII 0 followed by an ASCII 255.

Edit: Since you say the FadeLED example works, can you just take that example and add serial port functionality to it? For example, only fade after a serial character is received. This will at least get you confident that the serial port and leds can coexist in code you write. Also, I assume you're using the FadeLED example from here. If so, make sure you've set the correct pin number, as the reference code uses pin 9, but your sample code uses pin 11.

Another Edit: You say you receive the characters back properly from the Arduino, but you're using the pySerial ReadLine function, which should block until it sees a newline, and I don't see anything in the Arduino code that would generate a newline that wasn't sent to it first. To eliminate the number of variables in the question, try using HyperTerminal or a similar COM port program to send bytes to the Arduino, rather than your Python program. For example, you can send space characters (ASCII 32) and '}' characters (ASCII 125), which should be different enough to create a visible difference in the LED.

I would begin by establishing which side of the serial port harbours the problem.

First of all, can you drive the LED using a hard-coded loop on the Arduino. Does the LED change its brightness as expected?

Once that's out of the way, can you make the Arduino do something visibly correct when sending stuff manually from a terminal app (e.g. the one built into the Arduino IDE). This may require changes to the Arduino code.

Once that's working, I would then focus on the Python code, making sure you're talking to the right COM port etc.

"the write value should send the value to my board through USB"

If this is not a typo, then you can not use PySerial to access USB ports. There is pyUSB instead, and probably others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top