Home

Awesome

CS1237: MicroPython class for the CS1237 ADC

This is a short and simple class for the CS1237 ADC. It supports reading the ADC value, reading the temperature and configuring the various device modes.

Tested with MicroPython ports for RP2040, STM32, SAMD, i.MX RT (e.g. Teensy), ESP32, ESP8266, NRF52840 and W600. Approximate times for reading an ADC value:

The nrf port has the problem that the device cannot be configured when using the IRQ based driver. NRF52840 based devices have worked in the test using the polling driver. Otherwise the CS1237 operates at it's default mode, which is gain=128, rate=10, channel=0. pin.irq() seems not to work at the Renesas port, at least not with the tested EV-RA6M2 board. It can be configured, but refuses to work. The polling driver works.

Constructor

cs1237 = CS1237(clock_pin, data_pin[, gain=1, rate=10, channel=0])

This is the GPIO constructor. data_pin and clock_pin are the pin objects of the GPIO pins used for the communication. The arguments for gain, rate and channel are optional and can later be re-configured using the init() method.

Methods

cs1237.init(gain=None, rate=None, channel=None)

Configures or re-configures the ADC. All arguments are optional. Accepted values for gain are 1, 2, 64, 128 and for rate are 10, 40, 640, 1280. Channel values are:

At data rates of 640 and 1280 reading with a slow MCU may return wrong values, and configuring the device may fail. Then only a power cycle will reset the device. Since the current consumption of the CS1237 is low, it can be supplied by a GPIO output, making power cycling easy.

According to the test, Teensy 4.x and PYBD SF6 work fine at a rate of 1280. The RP2040, SAMD51 and Renesas RA6M2 work fine at a rate at 640 and can still be configured back.
ESP32, ESP8266, nrf52, SAMD21 and W600 can be configured once for a rate of 640, but cannot reset back to a lower rate and do not support temperature reading when set to the 640 rate.

result = cs1237.read()

result = cs1237()

Returns the actual reading of the ADC or temperature.

cs1237.get_config()

Returns the tuple of (gain, rate, channel) as read back from the ADC.

cs1237.config_status()

Returns True if a new configuration has been properly updated.

cs1237.calibrate_temperature(temp [, reference_value])

Set the calibration values for the temperature sensor. temp is the actual °C value. If both the temperature and a reference value are supplied, it is taken as the calibration tuple of the sensor. If not, the reference value is read from the sensor. The reference value can be obtained by configuring the sensor for temperature reading and calling cs1237.read().

cs1237.temperature(temp)

Return the actual temperature reading. The reference point has to be configured before using calibrate_temperature().

cs1237.power_down()

Set the CS1237 device to sleep mode.

cs1237.power_up()

Switch the CS1237 on again.

More methods exists but are used only internally by the CS1237 class.

Examples

# Connections:
# Pin # | CS1237
# ------|-----------
# 12    | data_pin
# 13    | clock_pin
#

from cs1237 import CS1237
from machine import Pin

data = Pin(12)
clock = Pin(13)

# Create a CS1237 instance with default values for gain, rate and channel
cs1237 = CS1237(clock, data)

# get ADC readings
value = cs1237.read()
# Alternative for reading the value
value = cs1237()

# change the gain
cs1237.init(gain=2)
value = cs1237.read()

# return the ADC settings
gain, rate, channel = cs1237.get_config()

# Alternative for showing the properties
print(cs1237)

# Calibrate the temperature reading
cs1237.calibrate_temperature(22.1)

# get the temperature
temp_celsius = cs1237.temperature()

# Calibrate the temperature reading using a known set point
# which has to be determined only once for a device.
cs1237.calibrate_temperature(20.0, 769000)

Files