ADC Read

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
alpha_pi
Posts: 4
Joined: Wed Nov 02, 2016 2:42 pm
Location: United States

ADC Read

Post by alpha_pi » Wed Nov 02, 2016 3:09 pm

This is my first post, so please forgive improper format if there is indeed any.

Question:
I would like to request an understanding, primarily in my coding format, for a Light Dependent Resistor(LDR, or any other value driven component) connected to X19 that will ultimately return a value. With this being said, there will obviously be a Max and Min value written into the code allowing me to Pull High, or Pull Low for instance X1.

How would I be able to continuously check the value of X19 ADC and compare my Max and Min values, and when they are satisfied, produce a result through an output Pin?

Here is an example of the code that I have been working with:

import pyb
from pyb import Timer
servo1 = pyb.Servo(1)
min_val = 90
max_val = 150
adc = pyb.ADC(pyb.Pin.board.X19)
tim = pyb.Timer(6, freq=10)
buf = bytearray(10)
bval = adc.read_timed(buf, tim)

if bval >= max_val:
servo1.speed(100) # or for example: pyb.Pin('X1', pyb.Pin.OUT_PP.high()
elif bval <= min_val:
servo1.speed(0) # or for example: pyb.Pin('X1', pyb.Pin.OUT_PP.high()

Thank you for any help received...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC Read

Post by dhylands » Wed Nov 02, 2016 9:36 pm

I would just add a loop, so something like this (untested):

Code: Select all

import pyb
servo1 = pyb.Servo(1)
min_val = 90
max_val = 150
adc = pyb.ADC(pyb.Pin.board.X19)
while True:
    bval = adc.read()
    if bval >= max_val:
        servo1.speed(100) # or for example: pyb.Pin('X1', pyb.Pin.OUT_PP.high()
    elif bval <= min_val:
        servo1.speed(0) # or for example: pyb.Pin('X1', pyb.Pin.OUT_PP.high()
    pyb.delay(100)
You don't need to use read_timed for this application.

User avatar
alpha_pi
Posts: 4
Joined: Wed Nov 02, 2016 2:42 pm
Location: United States

Re: ADC Read

Post by alpha_pi » Wed Nov 02, 2016 11:48 pm

Thank you very much, I was over thinking the process, as well as not quite understanding what to do.

I referenced an older post: http://forum.micropython.org/viewtopic.php?f=2&t=2160 this helped me understand better the format for what I was trying to do.

You responded, and please see pgm comment below:
while True:
adc_value = read_adc_value() # your suggesting read_adc_value() instead of adc.read() any difference?
if adc_value > upper_threshold:
set_gpio_high()
elsif adc_value < lower_threshold:
set_gpio_low()

Many thanks, I am receiving the results I wanted, thanks to resistors :)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC Read

Post by dhylands » Thu Nov 03, 2016 1:00 am

In the other post, I was referring to the "gist" of the algorithm, so read_adc_value was just a placeholder.

If you really wanted, you could implement:

Code: Select all

def read_adc_value():
    return adc.read()

User avatar
alpha_pi
Posts: 4
Joined: Wed Nov 02, 2016 2:42 pm
Location: United States

Re: ADC Read

Post by alpha_pi » Thu Nov 03, 2016 6:48 pm

Thank you Mr. Hylands, I appreciate you reviewing my first post and helping me understand better my issue and have resolve.

Measuring ambient light using and LDR is a bit tricky, especially due to the +/- variables in which the adc.read() will return under the same lighting conditions during a REPL adc.read() - given it is taken one right after the other. So my next step is to fixate on keeping this range within an integer value that will only fluctuate +/- 2 values.

I am delighted to be able to have access to such a forum as this.

Thank you once again!
>> alpha-pi

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ADC Read

Post by dhylands » Thu Nov 03, 2016 8:04 pm

I'd consider myself lucky if I could get values within +/- 2. Unless you have circuitry designed to eliminate noise, there is too much noise to get those types of readings.

And then there things like light bulbs which fluxuate at 60 Hz. Fast enough that a human eye can't see them. But your LDR is probably affected by it. 1 cycle of 60 Hz is 16 msec, which is a long time at the MCU scale. Even at 10 usec/bytecode that still represents 1600 bytecodes worth of time.

You should never expect to get rock solid values from real-world sensors.

Post Reply