Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: nonitus on Sep 11, 2015, 06:07 am

Title: [Help] Writing analog outputs with Python
Post by: nonitus on Sep 11, 2015, 06:07 am
Hi guys, I'm new to arduino, and I was wondering how can I, if it's possible, write an analog output using Python.

So far I've got:

connected = False
usbport = 'COM3'
com = serial.Serial(usbport,9600,timeout=1)
while not connected:
    serin = com.read()
    connected= True


for digital outputs, I just type com.write('PinNumber'), but I can't find anything for PWM.

Thanks!
Title: Re: [Help] Writing analog outputs with Python
Post by: Grumpy_Mike on Sep 11, 2015, 06:09 am
What code are you running on the Arduino?
Title: Re: [Help] Writing analog outputs with Python
Post by: nonitus on Sep 11, 2015, 06:16 am
I just set the outputs and begin the serial tx,

Serial.begin(9600);

The basic idea is to get my pc screen avg color , and send it to a RBG strip via an arduino.
Title: Re: [Help] Writing analog outputs with Python
Post by: Grumpy_Mike on Sep 11, 2015, 06:23 am
So you need to write code at both ends. Start by looking at the communications examples in the IDE.
You sent commands from Python down the serial port and you pick them up on the Arduino and then action them. These commands can be anything you want from a single letter to many words, you make them up. The command on the Arduino for PWM is the analogWrite function call. It only works on certain pins, they are marked on the Arduino with a ~ symbol.
Title: Re: [Help] Writing analog outputs with Python
Post by: nonitus on Sep 11, 2015, 06:34 am
Thanks for all the help so far!

I've just read the dimm example, and let me see if i got it right.
From python I should send the byte (com.write(ColorNumber)) that I have to read in Arduino (serial.Read()), is that correct? In that case, how do I send/receive 3 different bytes each time?
Does it all stay in a Stack or a Buffer?
Title: Re: [Help] Writing analog outputs with Python
Post by: Grumpy_Mike on Sep 11, 2015, 06:39 am
Yes that is right. To send three bytes just send them one after the other and then catch them at the other end.
Remember not to read more bytes than have arrived in the input buffer, use the serial available call to check all the bytes have been received before reading them.
Title: Re: [Help] Writing analog outputs with Python
Post by: nonitus on Sep 11, 2015, 06:45 am
Thank you for your help!
Title: Re: [Help] Writing analog outputs with Python
Post by: Robin2 on Sep 11, 2015, 09:29 am
This Python - Arduino demo (http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764) may be useful.

...R