11
votes

I'm trying to read potentiometer values from an arduino using python. But my serial read values are strange.

Python Code:

import serial

ser = serial.Serial('COM12')

print ( "connected to: " + ser.portstr )
count = 1                    

while True:
    for line in ser.read():
        print( str(count) + str( ': ' ) + str( line ) )
        count = count + 1


ser.close()

Arduino Code:

int potpin = 0;  // analog pin used to connect the potentiometer
int val = 0;       // variable to store the value coming from the sensor
int oldVal = 0;   // used for updating the serial print

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

void loop() 
{ 
  val = analogRead(potpin);             
  val = map(val, 0, 1023, 0, 179);     

  if( val != oldVal )
  {
    Serial.print(val);         // print the value from the potentiometer
    oldVal = val;
  }

  delay(100);
}

Some Python Output: This output came from a straight, slow increase on the potentiometer, I never turned it down at any point.

1: 56
2: 57
3: 49
4: 48
5: 49
6: 49
7: 49
8: 50
9: 49
10: 51

When I run the arduino serial terminal I get values that range from 0-179. Why isn't Python getting the correct values from the serial port?

Thanks

EDIT:

Solved the problem. 48-55 are the ascii values for 1-9 so it's a matter of changing the python code to print the character not the value. However this causes another problem in that it prints individual digits. for example, the number '10' comes in as a single '1' and '0'. This is simply solved by using Serial.write instead of Serial.print in the the arduino sketch. This also means that you'll be receiving a byte that is your number and not the ascii value of the number so you don't need to convert the read in line from a value to ascii.

Hope this helps.

1
+1 for posting your source code, and for posting the solution you found.jwygralak67
Instead of editing your question with the answer, you can post the solution as an answer and then accept your own answer. This makes it clear to people finding the question that there is a solution.Craig

1 Answers

2
votes

Let me try to offer a few comments that might be helpful to other folks with similar problems (even though this problem has been solved). First, try to run your Arduino sketch with the Serial Monitor a few times. You can find the Serial Monitor under Tools in the IDE menu. You can also type Ctrl-Shift-M to invoke the Serial Monitor.

Look at what shows up. It will frequently be quite helpful if your sketch tries to send data back via Serial.print(). A few notes. Make absolutely sure that the baud rate set inside the Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

The second note is critical. Bringing up the Serial Monitor forces a reset on the Arduino board. Your sketch starts over (always). This is a good thing because it gives you a fresh run each time. Note that you can force a reset, just by setting the baud rate to 9600 (even if it is already 9600). This lets you run many tests inside the Serial Monitor without having to restart the Serial Monitor each time.

As for your original problem, you should probably have some sort of data delimiters. For example, you could send the values like <53>. In Python you would scan for the '<' and then keep reading digits until you got the '>'. This will allow multiple digits to be sent as one numeric value.