CircuitPython Usage

It's easy to use the AM2320 sensor with CircuitPython and the Adafruit CircuitPython AM2320 module.  This module allows you to easily write Python code that reads the humidity and temperature from the sensor.

First wire up a AM2320 to your board exactly as shown on the previous pages for Arduino using an I2C connection.  Here's an example of wiring a ItsyBitsy M0 to the sensor with I2C:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA to sensor SDA

Next you'll need to install the Adafruit CircuitPython AM2320 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 introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_am2320.mpy
  • adafruit_bus_device

You can also download the adafruit_am2320.mpy from its releases page on Github.

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

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


To demonstrate the usage of the sensor we'll initialize it and read the humidity and temperature from the board's Python REPL.

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

import board
import busio
import adafruit_am2320
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_am2320.AM2320(i2c)

Remember if you're using a board that doesn't support hardware I2C (like the ESP8266) you need to use the bitbangio module instead:

i2c = bitbangio.I2C(board.SCL, board.SDA)

Now you're ready to read values from the sensor using any of these properties:

  • relative_humidity - The relative humidity measured by the sensor, this is a value from 0-100%.
  • temperature - The temperature measured by the sensor, a value in degrees Celsius.
print('Humidity: {0}%'.format(sensor.relative_humidity))
print('Temperature: {0}C'.format(sensor.temperature))

That's all there is to using the AM2320 with CircuitPython!

Below is a complete example that measures the sensor readings and prints them every two seconds.  Save this as on your board and open the REPL to see the output.  Remember to change to the bitbangio module if necessary for your board!

import time
import board
import busio
import adafruit_am2320

# can also use board.SDA and board.SCL for neater looking code!
i2c = busio.I2C(board.D2, board.D0)
am = adafruit_am2320.AM2320(i2c)

while True:
        print("Temperature: ", am.temperature)
        print("Humidity: ", am.relative_humidity)
    except OSError:
        # These sensors are a bit flakey, its ok if the readings fail
    except RuntimeError:
Last updated on Mar 07, 2018 Published on Mar 07, 2018