Sheik98
Posts: 5
Joined: Fri Nov 30, 2018 5:50 pm

Raspberry pi3 with arduino like slave to read 5 analogic signals

Mon Feb 25, 2019 10:32 pm

Like say the tittle in the post, how can I recibe 5 signals analogics connecting the arduino to my raspberry. Because I'm doing a robotic hand, and I'm using flex sensors in a glove to move the robotic hand from the raspberry. But I was reading about that, and just see can use from the arduino: Serial.println(analogRead(A0));
Serial.println(analogRead(A1));
Serial.println(analogRead(A2));
Serial.println(analogRead(A3));
Serial.println(analogRead(A4));
And recibe in the raspberry that like:

import serial
import time

arduino = serial.Serial("/dev/ttyACM0", baudrate=9600, timeout=3.0)

while True:
val = arduino.readline()
print(val)
arduino.close()

The problem is that val = arduino.readline() recibe all the 5 values like 1200 1213 900 680 800, like a string. How can I do to get each value and send it like a signal for 5 servos sg90

PhatFil
Posts: 1368
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Mon Feb 25, 2019 10:45 pm

as a python newbie myself a couple of things you could try, if you delay for 5 seconds or so between arduino serial prints your pi should have ample opportunity to read and flush single values from the serial interface between arduino updates.

However a more elegant way to deal with it would be to pull the values out of the string with python.
Perhaps https://www.pythonforbeginners.com/dict ... thon-split or
https://www.nltk.org/api/nltk.tokenize.html might be useful?

Sheik98
Posts: 5
Joined: Fri Nov 30, 2018 5:50 pm

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Tue Feb 26, 2019 1:08 am

PhatFil wrote:
Mon Feb 25, 2019 10:45 pm
as a python newbie myself a couple of things you could try, if you delay for 5 seconds or so between arduino serial prints your pi should have ample opportunity to read and flush single values from the serial interface between arduino updates.

However a more elegant way to deal with it would be to pull the values out of the string with python.
Perhaps https://www.pythonforbeginners.com/dict ... thon-split or
https://www.nltk.org/api/nltk.tokenize.html might be useful?
I reach separate the values with split. But when I recibe 3 times each value, a error jump and say:
x,y=val.split()
valueError: not enough values to unpack ( expected 2, got 0)


Can I do a excepcion? And how can I do it?

PhatFil
Posts: 1368
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Tue Feb 26, 2019 1:41 am

Sheik98 wrote:
Tue Feb 26, 2019 1:08 am
PhatFil wrote:
Mon Feb 25, 2019 10:45 pm
as a python newbie myself a couple of things you could try, if you delay for 5 seconds or so between arduino serial prints your pi should have ample opportunity to read and flush single values from the serial interface between arduino updates.

However a more elegant way to deal with it would be to pull the values out of the string with python.
Perhaps https://www.pythonforbeginners.com/dict ... thon-split or
https://www.nltk.org/api/nltk.tokenize.html might be useful?
I reach separate the values with split. But when I recibe 3 times each value, a error jump and say:
x,y=val.split()
valueError: not enough values to unpack ( expected 2, got 0)


Can I do a excepcion? And how can I do it?
https://www.pythonforbeginners.com/erro ... and-except
perhaps

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Tue Feb 26, 2019 2:48 am

Serial.print(analogRead(A1));
Serial.print ( ",");
Serial.print(analogRead(A2));
Serial.print ( ",");
Serial.print(analogRead(A3));
Serial.print ( ",");
Serial.println(analogRead(A4));
Without knowing why you are deleting my postings, I will not know how...

IanS
Posts: 234
Joined: Wed Jun 20, 2012 2:51 pm
Location: Southampton, England

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Tue Feb 26, 2019 10:49 am

Serial.print("1:");
Serial.print(analogRead(A1));
Serial.print ( ",2:");
Serial.print(analogRead(A2));
Serial.print ( ",3:");
Serial.print(analogRead(A3));
Serial.print ( ",4:");
Serial.print(analogRead(A4));
Serial.print ( ",");

Then it does not matter whether you read a subset or superset of the output as you can identify which reading is which.

scotty101
Posts: 3720
Joined: Fri Jun 08, 2012 6:03 pm

Re: Raspberry pi3 with arduino like slave to read 5 analogic signals

Tue Feb 26, 2019 10:53 am

There is an Arduino "library" called Firmata.

It is something you build and run on your arduino and it will allow a connected computer to read/write both digital and analogue values over the serial port.
Firmata has a software library for Python allowing you to read/write values on your Arduino from Python code.

Short tutorial about it here
https://raspberrypi-aa.github.io/session3/firmata.html
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Automation, sensing and robotics”