Skip to content

Instantly share code, notes, and snippets.

@boochow
Last active June 17, 2018 10:09
Show Gist options
  • Save boochow/1ee1edfc5087eec5c051b3c98d647288 to your computer and use it in GitHub Desktop.
Save boochow/1ee1edfc5087eec5c051b3c98d647288 to your computer and use it in GitHub Desktop.
class MCP3204:
def __init__(self, spi, cs):
self.spi = spi
self.cs = cs
def read(self, ch):
cmd = 6 | (ch >> 2)
par = (ch << 6) & 0xff
send = bytearray([cmd, par, 0])
recv = bytearray(3)
self.cs.value(0)
self.spi.write_readinto(send, recv)
self.cs.value(1)
return int.from_bytes(recv, 'big')
from machine import SPI, Pin
from time import sleep
spi=SPI(0)
cs=Pin(22, Pin.OUT)
adc=MCP3204(spi,cs)
while(True):
print(adc.read(0))
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment