On analog pin change?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
jan2000
Posts: 4
Joined: Tue Feb 14, 2017 11:57 am

On analog pin change?

Post by jan2000 » Mon Jun 05, 2017 3:30 pm

Hi guys! Is it possible to trigger something like a hard interrupt when the analog pin (A0) changes? I have a potentiometer connected to A0 on a WemosD1mini and want to know the value only when it changes - and would like to avoid asking for adc.read() every x ms.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: On analog pin change?

Post by pythoncoder » Mon Jun 05, 2017 3:58 pm

This is unsupported because it is not readily feasible. If you read an ADC periodically you will encounter "jitter" where the value changes slightly due to electrical noise or ADC imprecision. Reading it periodically is the way to proceed. This can be done in a loop, in response to a timer interrupt, or as a task using uasyncio. You might want to counter jitter by building in hysteresis so that an action is triggered if the value changes by more than a certain amount.
Peter Hinch
Index to my micropython libraries.

jan2000
Posts: 4
Joined: Tue Feb 14, 2017 11:57 am

Re: On analog pin change?

Post by jan2000 » Mon Jun 05, 2017 7:37 pm

Ok. That makes sense. Thank you!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: On analog pin change?

Post by mattyt » Tue Jun 06, 2017 2:09 am

Hard to know if it's appropriate for your application but it may be useful to take a look at Schmitt Triggers. They are a comparator circuit with hysteresis. The idea would be that you could feed your analog signal into the trigger and get a digital output change when the signal goes above a threshold and below a different threshold.

Very helpful if you're wanting to trigger only on changes, particularly if you're driving a micro into low-power modes.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: On analog pin change?

Post by Roberthh » Tue Jun 06, 2017 7:01 am

Digital schmitt-triggers have a fixed input trigger voltage of about 1/2 Vdd. If some other level is needed, a comparator has to be used, which can be set up to have an hysteresis (positive feedback). Then, almost arbitary switch levels are possible, and the output can trugger an interrupt. Note however, that the ESP8266 has a substantial time lag to interrupts, whether internal or external, than may go up to 500µs.
This external circuit could also wake up the ESP8266 from deep sleep.
If timing is not critical and deep sleep recovery is not needed, the method suggested by @pythoncoder is suitable. Getting an value from the ADC takes about 100 µs, the decision making would take another few µs, so as an estimation, this function would run for less than 200 µs, and a little bit faster, if you use the viper decorator. If you call that as an timer handler every x ms (x > 2), than that should be fine.

If power consumption is an issue, you can also stack the approaches - use the timer to wake up the device from deep sleep, and then check the ADC value upon recovery. But that's only useful if the sleep time is subtantially longer than the wake time.

User avatar
philwilkinson40
Posts: 63
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

Re: On analog pin change?

Post by philwilkinson40 » Sat Dec 16, 2017 1:55 pm

I would like to ask a related question to the forum, before I jump in and try and understand the terrifying world of digital Schmitt triggers....!

The use case is a bicycle traffic counter. A 2m long 5mm hollow rubber tube is connected to a MPX5010 differential air pressure sensor. This sensor is connected to ADC pin on a ESP8266 (however I could easily switch to another board).
By instantiating and reading a Pin object, at rest the ADC pin jitters at around 50.
When a bicycle passes over the rubber tube, and creates a pulse of air, the ADC reading always rises well over 100. The low values are presumably due to the 5V sensor.

Ideally, I do not want to poll the sensor as power needs to be minimised, so i hoped to trigger IRQ on a threshold ADC value. Time accuracy is not important in this use case.

Should I investigate using digital Schmitt triggers in this example?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: On analog pin change?

Post by pythoncoder » Mon Dec 18, 2017 8:01 am

As an approximation the values you are reading equate to millivolts. So you're trying to detect the difference between 50mV and something greater than 100mV. A Schmitt trigger would need to turn on at (say) 100mV and off at (say) 70mV. This is eminently possible but building it would require some circuit construction skills.

An op-amp such as a TLV2472 will run off a 3.3V supply and can be configured as a comparator with hysteresis. It will produce an output suitable for driving a GPIO pin. I could design a circuit if you are confident in constructing and testing it.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: On analog pin change?

Post by Roberthh » Mon Dec 18, 2017 9:16 am

It would be interesting so see the signals in an analog domain, e.g. witgh an oscilloscope. The peak pulse is likely very short and might not be captured well by the ADC.
As an alternative to the internal ADC you could also use an external one like ADS1015 or ADS1115, which can be purchased as a module for small money at ebay or even adafruit. These device are low power, have an internal amplifier and window comparator and able to create an interrupt pulse once a certain threshold is passed. The only drawback is, that they are not very fast and hardly able to capture pulses shorter than 500µs/2 ms.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: On analog pin change?

Post by pythoncoder » Mon Dec 18, 2017 6:49 pm

A bike ridden at 50km/h covers 14mm per ms. So I'd guess we're looking at pulses of 1ms or longer but it would be good to see actual figures. That chip sounds promising.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: On analog pin change?

Post by Roberthh » Mon Dec 18, 2017 8:53 pm

Speedy Pete on his bike, leaving behind a group of seniors, complaining about the young lads on a hurry.

Post Reply