Automation HAT ADC issue


#1

I’m working on a web based system to monitor broadcast station transmitter power. It’s based on Flask. My simple test Python3 script is occasionally (3 out of 10 samples) giving erroneous readings. Connecting the ADC input to the 5v terminal on the AH card, I sometimes get 0.0 or 25.6 instead of the 5v at that terminal.

Here’s the code:

#!/usr/bin/env python3

from flask import Flask, render_template
import datetime
import time

import automationhat
time.sleep(.25)

app = Flask(name)

@app.route("/")
def hello():
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
print(timeString)

power_now = automationhat.analog.one.read()
powerString = "{:.1f} volts".format(power_now)

templateData = {
    'title' : 'HELLO!',
    'time': timeString,
    'power': powerString
    }
return render_template('main.html', **templateData)

if name == “main”:
app.run(host=‘0.0.0.0’, port=8080, debug=True)

—end code—
The script should simply return the ADC reading, but some errors appear. What am I doing wrong?


#2

I’m afraid I’m away from the office, so I can’t replicate this, but from recollecting my previous experience with the ADC it doesn’t sound normal.

It looks like the input may be floating, and possibly coupled temporarily to the connected input. Are you sure you’re reading the same ADC input as you’ve connected your sensor to?


#3

I’ve connected the ADC input to a 5v terminal on the HAT using a jumper wire. Oddly, the voltage reading is stable with the same hookup running analog.py from Github. It’s a head scratcher. I’m not in a time bind as the project is an early stage. Thanks.


#4

Hmm, it’s quite possible something else is running- either in code or otherwise- that’s messing up the readings. I have a suspicion it could be some conflict between the sn3218 (that drives the LEDs) and the ADC, since they share the i2c bus and I seem to have been profoundly stupid here:

https://github.com/pimoroni/automation-hat/blob/5e32c9f6d31aa3741d8e291600cafe1f080873ba/library/automationhat/init.py#L310-L315

Which means that both of these threads are running simultaneously and potentially conflicting the bus.

If you get a moment, could you grab the code from GitHub, edit this file to remove the _auto_lights code, and install the library?

git clone https://github.com/pimoroni/automation-hat/
cd library
nano automationhat/__init__.py

Comment out the thread startup so it looks like:

#if is_automation_hat():
#    _t_auto_lights = AsyncWorker(_auto_lights)
#    _t_auto_lights.start()

And then install with:

sudo python setup.py install

And see if that fixes your ADC issues.


#5

Gadgetoid, I commented out the three lines (near the end of the file ahead of cleanup) and did the install for both Python and Python3. Although the led’s quit blinking and only the led for ADC 1 is illuminated, the 0.0, 5.0, 25.6 returns are still occurring.

To isolate the issue, I’ve assembled a new Pi3, and a second automationhat. This testbed omits the RTC card which also uses i2c. So with just the two boards and the SD card, I find a similar but not quite identical problem. Now the readings are 0.0, 5.1, and (very occasionally) 4.7. I remain stumped, but maybe the slightly different readings will provide a clue.

BTW – I’m using Python3 in case that makes a difference.


#6

One more thing. I just noticed that on the second testbed, no led’s are illuminated. That differs from the first, with one lit.


#7

Gadgetoid, After playing with this awhile, I’m inclined to think it’s a ‘Flask’ problem. Running the acquisition portion of the script on its own, no errors have been noted. I’m just starting to learn Flask, so i don’t know much about its internals. So I’ll look into that.

Thanks for the help. At least with your help I’m done with the bright, blinking leds!

Jim


#8

Ooooh. Disable reloader and that should fix it. There was a report about this somewhere else on the forums or github, I can’t remember where.

Aha! See: Explorer Hat pro flickering light when I turn it on using Flask endpoint


#9

Bingo! Thanks.

Thanks too for all the work you do for the Python community. Your dedicated efforts most especially help those of us learning the ropes.

Jim


#10

You’re most welcome! Happy hacking.