Problem with PySerial - Python - Arduino

Hello,

I recently bought an Arduino Uno and tried to make something funny.

I created a program in python that connects to a port and send desired data..
python:

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

The pde program is going to read the data via usb and trying to set analogWrite(pin, value)'s value.. but here it's the problem..nothing is happening, well i mean i don't know how to correctly set the value...

arduino:

      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);
            }
      }

can u help me please?
thank you in advance :slight_smile:

If you use the serial monitor of the IDE instead of python, does it work?

change the delay(10) in delay(1000) as every string from python may include a \n at the end being a value of 13 which almost will not lighten up the LED.

Can you share the complete code (python part)?

import wx, serial, time

portaCOM = 'COM5'

class Frame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, None, title=title, size=(285,110))
        panel = wx.Panel(self, -1)
        
        self.slider = wx.Slider(
            panel, 100, 127, 0, 255, (10, 10), (250, -1), 
            wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS 
        )
        self.slider.SetTickFreq(5, 1)

        self.Bind(wx.EVT_SLIDER, self.sliderUpdate)

    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"

app = wx.App(False)
frame = Frame(None, "Slider LED")
frame.Show()
app.MainLoop()

Ok I noticed that sending (char(0xff)) it's working , with the serial monitor as well, if I send a random char it's working..now how to do this by python?

Try a very simple python program first like the one on http://pyserial.sourceforge.net/shortintro.html and then try http://pyserial.sourceforge.net/examples.html#wxpython-examples

if I send a random char it's working..now how to do this by python?

You want python to send random characters?

Random char's in python...

Python

import random
for x in range(0,100):               # Do this 100 times
    print random.randrange(0,256)    # Print a random within 0-255
    sleep(0.25)                      # Wait 1/4 second

You can have it write the random byte to serial instead...

Thank you to everybody!!! I solved the problem just setting

  arduino.write(chr(pos))

and rebuilding the python in an easier and more correct version