Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/scruss/mcp4131
MicroPython module to control MicroChip's MC4131 SPI digital potentiometer
https://github.com/scruss/mcp4131
Last synced: 3 months ago
JSON representation
MicroPython module to control MicroChip's MC4131 SPI digital potentiometer
- Host: GitHub
- URL: https://github.com/scruss/mcp4131
- Owner: scruss
- License: mit
- Created: 2021-10-06T02:44:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-06T03:11:17.000Z (about 3 years ago)
- Last Synced: 2024-04-22T12:34:44.092Z (7 months ago)
- Language: Python
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micropython - mcp4131 - MicroPython module to control MicroChip's MCP4131 SPI digital potentiometer. (Libraries / IO)
README
# mcp4131
MicroPython module to control MicroChip's MC4131 SPI digital potentiometerfrom machine import Pin, ADC, SPI
import mcp4131
import time
a = ADC(0)
cs = Pin(17, Pin.OUT)
s = SPI(0, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
m = mcp4131.MCP4131(s, cs)
potval = m.set(0)
direction = 1
while True:
time.sleep(.1)
v = a.read_u16()
print("[ %5.3f %5.3f %5.3f ]" % (0.0, 3.3*v/65536, 3.3))
if direction == 1:
potval = m.inc()
else:
potval = m.dec()
if potval == 128:
direction = -1
if potval == 0:
direction = 1## Hardware overview
The MCP4131 is a simple single-wiper 7-bit digital potentiometer controlled by an SPI interface. It has no memory settings, so will forget its current setting when it loses power.
Spec/Datasheet: [mcp4131 | Microchip Technology](https://www.microchip.com/en-us/product/MCP4131)
## Methods
### Creation
m = mcp4131.MCP4131(s, cs)
Takes two arguments:
* *s*, an SPI object
* *cs*, a Pin object for the SPI Chip Select line.Note that on initialization, the potentiometer is set mid-way (64).
### set
Set the potentiometer to a value between 0 .. 128, inclusive. Returns the value of the potentiometer:
potval = m.set(0)
### getGets the potentiometer value, a number between 0 .. 128 inclusive.
potval = m.get()
### inc
Increments the value of the wiper. Will not go past 128. Returns a value between 0 .. 128, inclusive.
potval = m.inc()
### dec
Decrements the value of the wiper. Will not go past 0. Returns a value between 0 .. 128, inclusive.
potval = m.dec()
### value
Synonym for **get()**.
potval = m.value()
### ratioSimlar to **get()**, but returns a floating point value from 0.0 .. 1.0.
potratio = m.ratio()
## Note / Caveat
The MCP4131 uses a multiplexed data in/data out pin. Reading the wiper value is quite cumbersone, so I maintain the current potentiometer wiper value in a parallel variable. **get()** and others merely return the value of this variable.
## Author
© 2021, Stewart Russell, scruss.com