Some sample code

A project log for Raspberry Pi EVSE Hat

Use a Raspberry Pi to build an EV charging station

Nick SayerNick Sayer 09/25/2019 at 17:500 Comments

Reading the ADC:

import spidev

spi = spidev.SpiDev(), 0)
spi.max_speed_hz=1000000 # 1 MHz
spi.mode = 0
chan = 0 # 0..3 for MCP3004
buf = [1, 0x80 | (chan << 4), 0xff]
val = ((buf[1] & 0x3) << 8) | buf[2]
# val is output, from 0-1023.

While the ADC is rated (at Vdd=5V) for 3.6 MHz, it seems like going above 1 MHz makes its linearity suffer. Not sure why. Still, that's well over 40k samples per second.

To test the pilot, do this in a tight loop, say, 500 times capturing the low and high reading. In theory a pilot voltage of -12v when presented to the divider network should result in an ADC voltage of about 900 mV and a +12v should read as about 4.55v. Those two values from the ADC are 184 and 932 respectively. Between those two values the scale should be linear.