I2C Wiring

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

STEMMA QT Wiring

  •  Connect board VIN (red wire) to Metro 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
  • Connect board GND (black wire) to Metro GND
  • Connect board SCL (yellow wire) to Metro SCL
  • Connect board SDA (blue wire) to Metro SDA
  • Connect RH to Metro 5V
  • Connect RW to the A0 pin on the Metro, to allow us to measure the voltage

Breadboard Wiring

  •  Connect board VIN (red wire) to Metro 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
  • Connect board GND (black wire) to Metro GND
  • Connect board SCL (yellow wire) to Metro SCL
  • Connect board SDA (blue wire) to Metro SDA
  • Connect RH to Metro 5V
  • Connect RW to the A0 pin on the Metro, to allow us to measure the voltage

Library Installation

Once wired up, to start using the DS1841, you'll need to install the Adafruit_DS1841 library. The library is available through the Arduino library manager so we recommend taking that approach.

From the Arduino IDE, open up the Library Manager:

Click the Manage Libraries ... menu item, search for Adafruit DS1841, and select the Adafruit DS1841 library and click Install:

Follow the same process to install the Adafruit BusIO library.

Load Example

Open up File -> Examples -> Adafruit DS1841 -> ds1841_test and upload to your Metro wired up to the DS1841 breakout as shown above. Once you upload the code, you will see the wiper settings and measured voltage being printed when you open the Serial Monitor (Tools->Serial Monitor) at 15200 baud, similar to this:

Example Code

The following code comes with the library and illustrates the basic function of using the variable resistance of the DS1841 to change the voltage measured at pin A0:

// Basic demo for readings from Adafruit DS1841
#include <Wire.h>
#include <Adafruit_DS1841.h>
#define VOLTAGE_DIV_PIN A0
float wiperVoltage(void);
float wiper_voltage;

Adafruit_DS1841  ds;
void setup(void) {
  Serial.begin(115200);
  while (!Serial) delay(10);     // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit DS1841 test!");

  // Try to initialize!
  if (!ds.begin()) {
    Serial.println("Failed to find DS1841 chip");
    while (1) { delay(10); }
  }
  Serial.println("DS1841 Found!");
  Serial.print("Current VCC Voltage:"); Serial.print(ds.getVoltage()); Serial.println("mV");
  Serial.print("Temperature: ");Serial.print(ds.getTemperature());Serial.println(" degrees C");

  pinMode(VOLTAGE_DIV_PIN, INPUT);

}

void loop() {
  ds.setWiper(10);
  delay(1000);
  wiper_voltage = wiperVoltage();
  Serial.print("Voltage:");Serial.println(wiper_voltage);
  Serial.print("Temperature: ");Serial.print(ds.getTemperature());Serial.println(" degrees C");
  Serial.print("Wiper: ");Serial.print(ds.getWiper());Serial.println(" LSB");
  Serial.println("");
  
  ds.setWiper(120);
  delay(1000);
  wiper_voltage = wiperVoltage();
  Serial.print("Voltage:");Serial.println(wiper_voltage);
  Serial.print("Temperature: ");Serial.print(ds.getTemperature());Serial.println(" degrees C");
  Serial.print("Wiper: ");Serial.print(ds.getWiper());Serial.println(" LSB");
  Serial.println("");
}

float wiperVoltage(void) {
  float wiper_value = analogRead(VOLTAGE_DIV_PIN);
  wiper_value *= 5.0;
  wiper_value /= 1024;
  return wiper_value;
}

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

This page (Arduino) was last updated on Sep 15, 2021.

Text editor powered by tinymce.