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 explicitly

Methods

MethodReturnsDescription
adc.read(ch)int (0.1% units)Ratio of input voltage / supply voltage
adc.read_raw(ch)int 0–4095Raw 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 rangeContent
0x10–0x17ADC raw data
0x20–0x27Input voltage
0x29Output voltage (Grove supply, usually 3.3V)
0x30–0x37Input 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