-
Notifications
You must be signed in to change notification settings - Fork 74
Expose NINA command handlers for Digital Read and Analog Read #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… 12-bit to 0-65535.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req'ing some small modifications
@anecdata Those pins aren't exposed on the ESP32 AirLift FeatherWing, did you solder directly to the pads? Could you attach a pic. of your test setup. |
These features will not work on Airlift FeatherWing, Airlift Breakout, Bitsy Airlift add-on, or all-in-ones without surgery. They're easiest on a standalone full-pinout ESP32 like the ESP32 Feather (see photo below) or the ESP32 Huzzah Breakout (or probably the TinyPICO too). ESP32SPI pins now available for input and output: https://gist.github.com/anecdata/80aa1b95933c0d37828c0c177f229d73 Main test rig: |
Released nina-fw 1.5.0 (https://github.com/adafruit/nina-fw/releases/tag/1.5.0), merging this PR in so CircuitPythonistas can start readin'! Thanks for the work on this @anecdata. |
Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 3.1.0 from 3.0.2: > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#80 from anecdata/Kraken_II > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#82 from adafruit/dherrada-patch-1 > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#81 from adafruit/dherrada-patch-1 Updating https://github.com/adafruit/Adafruit_CircuitPython_ST7789 to 1.2.0 from 1.1.3: > Merge pull request adafruit/Adafruit_CircuitPython_ST7789#12 from makermelissa/master > Merge pull request adafruit/Adafruit_CircuitPython_ST7789#11 from adafruit/dherrada-patch-1
Tested digital & analog Read on TinyPICO + ItsyBitsy M4, and updated available GPIO pins for TinyPICO at https://gist.github.com/anecdata/80aa1b95933c0d37828c0c177f229d73 |
This PR adds two command handlers to ESP32SPI, changing files in the ESP32SPI CircuitPython library corresponding to a PR for adding the two handlers in the NINA firmware. Addresses #76
•
set_digital_read(pin)
is easy: pin in, boolean return.•
set_analog_read(pin, atten)
takes two parameters, pin and an attenuation constant, and returns a 12-bit-resolution value (0-4095) as an integer in the range 0-65535 (to be consistent with CircuitPython analogio).Only ADC1 pins are allowed since ADC2 can't be used when Wi-Fi is running. Not all eight of the ADC1 pins can be used. 37 & 38 aren't exposed on Adafruit boards. 36 and 39 may be dedicated to the Hall Effect sensor (needs further investigation whether they can be used independently for arbitrary ADC). 33 is used for ESP32SPI Busy/!Rdy in our NINA builds. That leaves 32 and 34 (A2) for user connections. Also, 35 (A13) is connected to the battery via a voltage divider and will give half the battery voltage.
The ESP32 ADC is 12 bits by default (can potentially be adjusted to 11, 10, or 9). This PR sticks to the 12-bit default.
Test environment
• Adafruit Feather M4 Express with samd51j19
• CircuitPython 5.0.0-alpha.4 on 2019-09-15
• CircuitPython Library Bundle 20191012
• ESP32 Feather, NINA firmware 1.4.0 as base
• ESP32 digital out (21) connected to digital in (12)
• ESP32 pin 32 connected to trim pot to 3.3v
• ESP32 pin 35 internally connected to the battery via a voltage divider
• M4 & ESP32 connected to host via their respective USBs for output
In testing I saw roughly ±5% or so measurement variation between ESP32 analog read values vs. my meter. Possible sources identified: noise from breadboard, jumpers, SPI, etc.; chosen attenuation value; and ADC calibration (or lack thereof), damaged ESP32, bad meter. I tried delays, and also capacitors on the inputs and multi-sampling per Espressif suggestion, to little effect. It does not seem to work well to connect an ESP32 analog output to an ESP32 analog input, so at times I used the M4 to generate test voltages.
The voltage regulator on the ESP32 Feather is the AP2112-3.3, and the Diodes Inc datasheet says the regulator Output Voltage Accuracy is ±1.5% @ 25°C (±100 ppm/°C).
Sample test code
Sample test results (annotated)
The CircuitPython decimal and hex values displayed will be 16 times the values shown on the ESP32 debug output due to the 16-bit value range (0-65535) vs. 12-bit value range (0-4095) difference.
Attenuation
The attenuation constant is one of four, from 0dB (Espressif default) to 11dB (our default). 11dB was chosen as CircuitPython default since it will allow the fullest range of analog read voltage inputs (I tested also at 0dB and it didn't seem to make a difference in measurement variation):
"11dB attenuation (ADC_ATTEN_DB_11) gives full-scale voltage 3.9V"*
*At 11dB attenuation the maximum voltage is limited by VDD_A, not the full scale voltage.
Espressif also notes, "For maximum accuracy, use the ADC calibration APIs and measure voltages within these recommended ranges: ...11dB attenuation (ADC_ATTEN_DB_11) between 150 to 2450mV"
Calibration
Automatically measuring Vref requires Wi-Fi to be off (uses ADC2), so this isn't practical in our application, and trying to pass in a user-supplied Vref will not work (I tried) on chips newer than start of 2018 due to built-in values in eFuse taking precedence. I've added the basic characterization code, which will use eFuse values or 1100mV default if no eFuse. Adding the characterization code didn't seem to make a difference in measurement variation.
Reference
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/adc.html#
cc: @brentru