Problem with PySerial - Python - Arduino


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 ->

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...


      const int ledPin = 11;
      byte brightness;

      void setup(){
            pinMode(ledPin, OUTPUT);

      void loop(){
                  brightness =;
                  analogWrite(ledPin, brightness); //LED doesn't refresh the brightness

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

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()
        print arduino.readline()

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

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

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

Try a very simple python program first like the one on and then try

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

You want python to send random characters?

Random char's in 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


and rebuilding the python in an easier and more correct version