swilker
Posts: 12
Joined: Mon Dec 28, 2015 4:05 am

Stuck Python to Arduino Serial

Sat Jan 23, 2016 5:02 am

Running Ubuntu on a Pi 2 wrote this code in python:

import serial
test=serial.Serial("/dev/ttyACM0",9600)
test.write('3')

It's sending something, but I need it to send the number 3 to the Arduino and it's definitely not sending that.
I've tested the Arduino code from my MAC and I open a serial window type in a 3 and get three blinks.

Please Help been stuck for hours probably an easy fix.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: Stuck Python to Arduino Serial

Sat Jan 23, 2016 6:54 am

Try

test.write(bytes("3".encode('ascii')))

swilker
Posts: 12
Joined: Mon Dec 28, 2015 4:05 am

Re: Stuck Python to Arduino Serial

Sat Jan 23, 2016 9:56 pm

Thanks you for your help, however I had already tried that. Just in case I did it again, still doesn't work. Light blinks, but blinking not related to number sent.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: Stuck Python to Arduino Serial

Sat Jan 23, 2016 9:59 pm

What light doesn't blink?

What code is in the Arduino?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Stuck Python to Arduino Serial

Sat Jan 23, 2016 11:38 pm

That serial port address looks wrong to me. Shouldn't it be AMA0? Working from memory here as I'm not at my computer :D
Alex Eames RasPi.TV, RasP.iO

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Stuck Python to Arduino Serial

Sat Jan 23, 2016 11:51 pm

alexeames wrote:Shouldn't it be AMA0?
Yes, it should.

Code: Select all

test = serial.Serial()
test.baudrate = 9600
test.timeout = 0
test.port = "/dev/ttyAMA0"
Before sending message you should open the port:

Code: Select all

test.open()
Then if you are using Python 2:

Code: Select all

test.write("3")
But for Python 3 it will be:

Code: Select all

test.write("3".encode()

swilker
Posts: 12
Joined: Mon Dec 28, 2015 4:05 am

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 3:10 am

test.open() gives an error
Yes python 2
ls /dev/tty* returns ACM0 not AMA0 as the new connection
test.write("3".encode()) same result, light blinks once

const int ledPin = 13;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
if (Serial.available()) {
light(Serial.read() – ‘0’);
}
delay(500);
}
void light(int n){
for (int i = 0; i < n; i++) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
}

swilker
Posts: 12
Joined: Mon Dec 28, 2015 4:05 am

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 3:41 am

Solution:
import serial
import time
test= serial.Serial("/dev/ttyACM0",9600)
time.sleep(2)
test.write("3".encode('ascii'))

requires time.sleep(2) to allow arduino to reset on connect. Found it on OscarLiang.net
http://blog.oscarliang.net/connect-rasp ... usb-cable/

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 7:25 am

alexeames wrote:That serial port address looks wrong to me. Shouldn't it be AMA0? Working from memory here as I'm not at my computer :D


an Arduino R3 uses ACM0 when connected by usb.

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 10:24 am

Glad you were able to sort it out. I thought that you are trying to use Rx/Tx pins on GPIO to connect, not USB port. But USB port is safer option due to the voltage difference (5 V Arduino vs. 3.3 V RPi).

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 6:41 pm

alexeames wrote:That serial port address looks wrong to me. Shouldn't it be AMA0? Working from memory here as I'm not at my computer :D
Not if the Arduino is connected to the USB it isn't. It's /dev/ttyACMx. /dev/ttyAMA0 is the Raspberries UART on pins #8 & #10, you'd need a level shifter to connect a 5V0 Arduino to that.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Stuck Python to Arduino Serial

Sun Jan 24, 2016 7:35 pm

gordon77 wrote:
alexeames wrote:That serial port address looks wrong to me. Shouldn't it be AMA0? Working from memory here as I'm not at my computer :D


an Arduino R3 uses ACM0 when connected by usb.
Thanks. That's good to know. The OP didn't say he'd connected by USB or what kind of arduino it was so I just chipped in with what I knew in case it was an obvious "is there petrol in the tank" mistake :)
Alex Eames RasPi.TV, RasP.iO

Return to “General discussion”