5

Ok I apologize as I have edited this quite a bit, but I have narrowed down the problem a bit more. For some reason I can't transfer values from my arduino analog pins??

I am trying to send values from my arduino to raspberry pi over serial (USB cable).

Here is very simple code:

int someVal = 0;
int inputPinA = A0;
int pin = 0;

void setup() {
Serial.begin(9600); 
}

void loop() {
someVal++;
Serial.println(someVal);
delay(100);
pin = analogRead(inputPinA);
Serial.println(pin);
}

When my arduino is plugged into my windows PC, running the arduino IDE, this prints exactly as expected. When my arduino is plugged into my raspberry pi, someval is printed normal, but pin is just 0 repeating... I tried changing int pin = 1; at the beginning to see if it would print 1 instead of 0, but nothing. I also tried Serial.println(String(pin)); which still printed 0.... what is going on here, I am stumped.

Raspberry pi code:

import serial 

ser = serial.Serial('/dev/ttyACM0', 9600,8,'N',1,timeout=None)
ser_bytes = 0
decoded_bytes = 0


while True:
    ser_bytes = ser.readline()
    recoatVal = str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
    print(recoatVal)

I also simplified my python code to:

import serial 

ser = serial.Serial('/dev/ttyACM0', 9600,8,'N',1,timeout=None)
ser_bytes = 0

while True:
    ser_bytes = ser.readline()
    print(ser_bytes)

Which just returns(prints):

b '1\r\n' <----someVal

b '0\r\n'<----pin

Also, some people were asking for what the arduino outputs:

1
245
2
556
3
433
4
545
...and so on
6
  • 1
    upvote for thinking about where to post your question and for not posting at multiple places
    – jsotola
    Nov 29, 2019 at 19:33
  • @MattG, Ah let me see. (1) Rpi USB UART/serial program can send something to Arduino which prints it in its own Arduino terminal. (2) Arduino can send something serial to Rpi, which receives it , and then sends that something back to Arduino which receives it and prints it in its Arduino terminal. (3) Now the TROUBLE: This time Arduino first does some averaging thing and then sends the averaged result to Rpi, But this time Rpi does not receive anything. Why? Do I understand the problem correctly?
    – tlfong01
    Nov 30, 2019 at 1:33
  • 1
    I edited question with hopes of the problem being more clear
    – MattG
    Nov 30, 2019 at 3:16
  • 1
    You should include in you question what the Arduino outputs. The construct str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8")) looks bizarre, and is probably obscuring whatever is input. This looks more like a python coding question rather than a Pi question.
    – Milliways
    Nov 30, 2019 at 3:51
  • 1
    Ok I figured it out... but need someone to actually explain it, as I don't think this counts as an answer. If I change int pin = 0 to float pin = 0 it works.... no idea why
    – MattG
    Nov 30, 2019 at 21:26

1 Answer 1

0

MattG,

analogRead a returns a float.. and the print() was probably getting confused too.

Leave out the int inputPinA = A0; because you only need to put A0 in the AnalogRead.

Make int pin = 0; a float, or better use float pin_value = 0.0; for clarity

Then the stuff in your loop becomes:

pin_value = analogRead(A0); Serial.println(pin_value);

Dave

1
  • I don't understand this answer. First, analogRead returns an integer, not a float. And second, what's confusing about printing a float? Jul 29, 2020 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.