The adafruitio_15_temp_humidity example uses digital pin 2 by default on all boards, and that can be modified if needed by changing the DATA_PIN define.

// pin connected to DH22 data line
#define DATA_PIN 2

The next chunk of code creates an instance of the DHT class, and also sets up feed instances for the temperature and humidity feeds.

// create DHT22 instance
DHT_Unified dht(DATA_PIN, DHT22);

// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");

The setup function initializes the DHT22 sensor, and also connects your feather to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.

void setup() {

  // start the serial connection

  // wait for serial monitor to open
  while(! Serial);

  // initialize dht22

  // connect to
  Serial.print("Connecting to Adafruit IO");

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {

  // we are connected


Next, we have the main loop() function. The first line of the loop function calls; this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.

void loop() {

  //; is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  //, and processes any incoming data.;

The next chunk of code inside the loop() checks the current DHT22 temperature value, and saves the value in the celsius and fahrenheit variables.

We then print both celsius and fahrenheit to the Arduino Serial Monitor, and save the fahrenheit value to the temperature feed on Adafruit IO.

  sensors_event_t event;

  float celsius = event.temperature;
  float fahrenheit = (celsius * 1.8) + 32;

  Serial.print("celsius: ");

  Serial.print("fahrenheit: ");

  // save fahrenheit (or celsius) to Adafruit IO

If you prefer to log celsius values, you can modify the call to the save() function.


The final chunk of the loop() function requests a humidity reading from the DHT22, and prints the value to the Arduino Serial Monitor. We also save the humidity value to the humidity feed on Adafruit IO.


  Serial.print("humidity: ");

  // save humidity to Adafruit IO

  // wait 5 seconds (5000 milliseconds == 5 seconds)


Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.

Connecting to Adafruit IO....

Adafruit IO connected.

You should now see the temperature and humidity values being sent to Adafruit IO.

celsius: 18.30C
fahrenheit: 64.94F
humidity: 34.90%
celsius: 18.20C
fahrenheit: 64.76F
humidity: 35.40%

Check your dashboard on Adafruit IO, and you should see the line chart update with the changes in temperature and humidity.

This guide was first published on Feb 20, 2017. It was last updated on Nov 08, 2023.

This page (Arduino Code) was last updated on Feb 20, 2017.

Text editor powered by tinymce.