0

I am trying to read an ads1115 monitoring a current shunt so it's reading in millivolts. All I need is to be able to read the serial output from the arduino serial. When I bring the arduino up with the IDE in macos sierra I get changing one or two digit values. I'm confused, am I reading the serial wrong in the Python? I know I have the correct port and only one arduino is plugged in.

Arduino code:

Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
void setup(void)
{
  Serial.begin(9600);
   ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV

  ads.begin();
}

void loop(void)
{
  int16_t results;
  results = ads.readADC_Differential_0_1();  
  Serial.println(results);
  delay(1000);
}

Python code on RPi:

import io
import sys
import serial
import time

logfilename = 'temp' + time.strftime("%Y-%m-%d")
file = open('/home/pi/' + logfilename + '.log', 'w+')

ser = serial.Serial('/dev/ttyACM0', 9600)
ser.flushInput()
ser.flushOutput()
while True :
    try:
        bytesToRead = ser.inWaiting()
        state=ser.read(bytesToRead)
        file.write( "%s\n" % (state) )
        time.sleep(1)
    except ValueError:
        print "error"

file.close()

What is contained in the file

pi@pi:~$ more ~/temp2017-04-01.log

0


0


0


0


0


0

What I see from the IDE serial monitor:

11
12
0
18
2
0
3
0
14
6

Edit: I didn't figure it out, but I did get an ethernet shield and webserver to work. I'm still curious why this didn't work.

0

The Arduino outputs each result with a line terminator.

I suggest you use a blocking read line at the Pi.

while True:
   line = ser.readline()
   file.write("%s\n" % (line))
| improve this answer | |
  • I switched to the ser.inWaiting because I was also getting 0s with the ser.readline and thought it might be a buffer issue. – Rich Apr 1 '17 at 19:28
  • Should probably also set timeout if you use readline() – scruss May 31 '17 at 15:24
0

First, are you sure you connected the cable right? Also, I think the Raspi comes with the serial configured for terminal emulation, you have to do something to make it available for your programs. See this: How do I make serial work on the Raspberry Pi3

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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