Last active
October 30, 2016 01:05
-
-
Save glennklockwood/15a2740cf5a83e1e281c278fa8128c2e to your computer and use it in GitHub Desktop.
MCP3008 and MCP41010 Transistor Experimentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Vary resistance on a digital potentiometer and measure the effect using | |
an analog-digital converter""" | |
import spi # from https://github.com/glennklockwood/raspberrypi | |
import time | |
### use two independent SPI buses, but daisy chaining then is also valid | |
adc = spi.SPI(clk=18, cs=25, mosi=24, miso=23, verbose=False) | |
digipot = spi.SPI(clk=19, cs=13, mosi=26, miso=None, verbose=False) | |
### iterate over all possible resistance values (8 bits = 256 values) | |
for resist_val in range(256): | |
### set the resistance on the MCP41010 ############################# | |
cmd = int("00010001", 2) | |
# make room for resist_val's 8 bits | |
cmd <<= 8 | |
digipot.put(cmd|resist_val, bits=16) | |
### wait to allow voltage transients to subside | |
time.sleep(0.2) | |
### get the voltage from the MCP3008 ############################### | |
voltages = [0, 0, 0] | |
for channel in range(len(voltages)): | |
# set the start bit, single-ended mode bit, and 3 channel select bits | |
cmd = int("11000", 2) | channel | |
# read 1 null bit, then 10 data bits | |
cmd <<= 10 + 1 | |
value = adc.put_get(cmd, bits=16) | |
# mask off everything but the last 10 read bits | |
value &= 2**10 - 1 | |
voltages[channel] = 3.3 * value / 1023.0 | |
### 10000.0 because MCP41010 is a 10 Kohm digital pot | |
print "%4.2f %4.2f %4.2f %5d" % (voltages[0], voltages[1], voltages[2], | |
10000.0 * resist_val / 255.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment