Source: grove.adc — grove.py 0.6 Documentation
Overview
grove.adc.ADC is the Python class for reading analog values from the Grove Base Hat for Raspberry Pi.
The Grove Base Hat has a built-in STM32F030F4 microcontroller that acts as an external ADC. The Pi communicates with it over I2C (not SPI).
import time
from grove.adc import ADC
adc = ADC()
while True:
print(adc.read_voltage(0)) # Read channel 0 (A0 port) in mV
time.sleep(1)ADC Class
from grove.adc import ADC
adc = ADC() # default I2C address 0x04
# adc = ADC(address=8) # specify address explicitlyMethods
| Method | Returns | Description |
|---|---|---|
adc.read(ch) | int (0.1% units) | Ratio of input voltage / supply voltage |
adc.read_raw(ch) | int 0–4095 | Raw 12-bit ADC value |
adc.read_voltage(ch) | int (mV) | Actual voltage on channel |
ch = channel number 0–7, corresponding to ports A0–A7 on the hat.
Properties
adc.name— hat name string ('Grove Base Hat RPi'or'Grove Base Hat RPi Zero')adc.version— firmware version int
I2C Register Map (reference)
| Register range | Content |
|---|---|
| 0x10–0x17 | ADC raw data |
| 0x20–0x27 | Input voltage |
| 0x29 | Output voltage (Grove supply, usually 3.3V) |
| 0x30–0x37 | Input voltage / output voltage ratio |
Usage in joystick (L4)
from grove.adc import ADC
from gpiozero import Button
import subprocess
adc = ADC()
btn = Button(22) # SW/SEL pin of joystick connected to digital port D22
if __name__ == "__main__":
while True:
x_mv = adc.read_voltage(0) # VRx → A0
y_mv = adc.read_voltage(1) # VRy → A1
# Map voltages to mouse movement delta...
subprocess.run(['ydotool', 'mousemove', '-x', str(dx), '-y', str(dy)])See Also
- GPIO and reTerminal topic
- ADC concept
- enable-i2c source (I2C must be enabled before grove.adc works)
- lab4-grove-joystick source