CircuitPython Microcontroller Wiring

Wiring the DS1841 to communicate with your microcontroller is straightforward thanks to the I2C interface. For these examples, we can use a Metro or a Feather to measure the voltage changes as the DS1841 adjusts its resistance. The instructions below reference a Feather, but the same applies to a Metro.

STEMMA QT Wiring

  • Connect board Vcc (long red wire) to Feather 3.3V 
  • Connect board GND (black wire) to Feather GND
  • Connect board SCL (yellow wire) to Feather SCL
  • Connect board SDA (blue wire) to Feather SDA
  • Connect RH to Vcc (short red wire)
  • Connect RW to the A0 pin on the Feather, to allow us to measure the voltage

Breadboard Wiring

  • Connect board Vcc (long red wire) to Feather 3.3V 
  • Connect board GND (black wire) to Feather GND
  • Connect board SCL (yellow wire) to Feather SCL
  • Connect board SDA (blue wire) to Feather SDA
  • Connect RH to Vcc (short red wire)
  • Connect RW to the A0 pin on the Feather, to allow us to measure the voltage

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

Note that because the Raspberry Pi does not include any pins with analog to digital converters (ADCs) to read the voltage that will change on the DS1841's RW pin, you will need to use a multimeter to measure the voltage between the RW pin and GND.

If you are new to using a multimeter to measure voltage, check out ladyada's guide to multimeters which has a section on using your multimeter to measure voltage

STEMMA QT Wiring

  • Connect board Vcc (long red wire) to Pi 3.3V 
  • Connect board GND (black wire) to Pi GND
  • Connect board SCL (yellow wire) to Pi SCL
  • Connect board SDA (blue wire) to Pi SDA
  • board Vcc to board RH (short red wire)
  • Multimeter Positive Lead to sensor RW
  • Multimeter Negative Lead to sensor GND

Breadboard Wiring

  • Connect board Vcc (long red wire) to Pi 3.3V 
  • Connect board GND (black wire) to Pi GND
  • Connect board SCL (yellow wire) to Pi SCL
  • Connect board SDA (blue wire) to Pi SDA
  • board Vcc to board RH (short red wire)
  • Multimeter Positive Lead to sensor RW
  • Multimeter Negative Lead to sensor GND

CircuitPython Installation of DS1841 Library

You'll need to install the Adafruit CircuitPython DS1841 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_ds1841.mpy
  • adafruit_bus_device
  • adafruit_register

Before continuing make sure your board's lib folder or root filesystem has the adafruit_ds1841.mpy, adafruit_bus_device, and adafruit_register files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of DS1841 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-ds1841

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython Usage

Because of hardware differences, Python Computer users should follow the "Python Usage" examples in the next section

To demonstrate the usage of the potentiometer we'll initialize it and set the wiper value to change the voltage on the WH pin. We will then use the A0 pin to take an analog reading of the voltage on the WH pin.

Run the following code to import the necessary modules and initialize the I2C connection with the potentiometer:

Download: file
import board
import busio
from analogio import AnalogIn
import adafruit_ds1841
i2c = busio.I2C(board.SCL, board.SDA)
ds1841 = adafruit_ds1841.DS1841(i2c)
wiper_output = AnalogIn(board.A0)

With the driver initialized, we can the set the wiper value using the wiper property. Then we can take an ADC reading to measure the voltage, and convert the raw ADC value to a human-readable value

Download: file
ds1841.wiper = 127
print("Wiper set to %d" % ds1841.wiper)
voltage = wiper_output.value
voltage *= 3.3
voltage /= 65535
print("Wiper voltage: %.2f V" % voltage)

We can then set the wiper to a different value and see how it changes the measured wiper voltage

Download: file
ds1841.wiper = 0
print("Wiper set to %d" % ds1841.wiper)
voltage = wiper_output.value
voltage *= 3.3
voltage /= 65535
print("Wiper voltage: %.2f V" % voltage)

Full CircuitPython Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

from time import sleep
import board
import busio
from analogio import AnalogIn
import adafruit_ds1841

####### NOTE ################
# this example will not work with Blinka/rasberry Pi due to the lack of analog pins.
# Blinka and Raspberry Pi users should run the "ds1841_blinka_simpletest.py" example

# WIRING:
# 1 Wire connecting  VCC to RH to make a voltage divider using the
#   internal resistor between RH and RW
# 2 Wire connecting RW to A0

# setup of the i2c bus giving the SCL (clock) and SDA (data) pins from the board
i2c = busio.I2C(board.SCL, board.SDA)
# create the ds1841 instance giving the I2C bus we just set up
ds1841 = adafruit_ds1841.DS1841(i2c)

# set up an analog input, selecting the A0 pin
wiper_output = AnalogIn(board.A0)

while True:

    # set th
    ds1841.wiper = 127
    print("Wiper set to %d" % ds1841.wiper)
    voltage = wiper_output.value
    voltage *= 3.3
    voltage /= 65535
    print("Wiper voltage: %.2f V" % voltage)
    print("")
    sleep(1.0)

    ds1841.wiper = 0
    print("Wiper set to %d" % ds1841.wiper)
    voltage = wiper_output.value
    voltage *= 3.3
    voltage /= 65535
    print("Wiper voltage: %.2f V" % voltage)
    print("")
    sleep(1.0)

    ds1841.wiper = 63
    print("Wiper set to %d" % ds1841.wiper)
    voltage = wiper_output.value
    voltage *= 3.3
    voltage /= 65535
    print("Wiper voltage: %.2f V" % voltage)
    print("")
    sleep(1.0)

Python Usage

Because the Raspberry Pi and many other similar devices do not include the hardware for measuring analog voltages, you will have to use a multimeter to measure the voltage on the RW pin.

To start, similar to above we will import the needed modules and initialize the driver

Download: file
import board
import busio
import adafruit_ds1841

i2c = busio.I2C(board.SCL, board.SDA)
ds1841 = adafruit_ds1841.DS1841(i2c)

Once the driver has been initialized, we can use it to set the value of the wiper

Download: file
ds1841.wiper = 127

You can then use your multimeter or other voltage measuring device to check the voltage across GND and RW. It should be the same as shown in the CircuitPython example above, approximately 0.3V


Next, change the wiper value again and measure the voltage on the RW pin

Download: file
ds1841.wiper = 0

The voltage on the RW pin should be the same as shown in the CircuitPython example above, this time approximately 2.8V

Full Python Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

####### NOTE ################
# This example is meant for use with Blinka/rasberry Pi due to the lack of analog pins.
# CircuitPython board users should run the "ds1841_simpletest.py" example

# WIRING:
# 1 Wire connecting  VCC to RH to make a voltage divider using the
#   internal resistor between RH and RW

# As this code runs, measure the voltage between ground and the RW (wiper) pin
# with a multimeter. You should see the voltage change with each print statement.
from time import sleep
import board
import busio
import adafruit_ds1841

i2c = busio.I2C(board.SCL, board.SDA)
ds1841 = adafruit_ds1841.DS1841(i2c)

while True:
    ds1841.wiper = 127
    print("Wiper value set to 127")
    sleep(5.0)

    ds1841.wiper = 0
    print("Wiper value set to 0")
    sleep(5.0)

    ds1841.wiper = 63
    print("Wiper value set to 63")
    sleep(5.0)

This guide was first published on Jul 07, 2020. It was last updated on Jul 07, 2020.

This page (Python & CircuitPython) was last updated on Apr 13, 2021.

Text editor powered by tinymce.