Built-in project · Arduino Uno R3

Temperature and light on one I2C bus, on an Arduino Uno

A TMP117 temperature sensor and a VEML7700 light sensor share the Uno’s SDA and SCL at their own addresses, and the serial monitor prints both every second. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $44. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sharing one I2C bus between two devices
  • I2C addresses
  • Using sensor libraries from Adafruit
  • Reading temperature and light in real units
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

Two sensors share the same two wires. The TMP117 measures temperature and the VEML7700 measures light. Both connect to SDA and SCL, which on the Uno are A4 and A5.

I2C lets many parts share a bus as long as each answers at its own address. The TMP117 is at 0x48 and the VEML7700 at 0x10. The sketch starts each one, and stops with a message if either does not answer.

After that, every second it prints the temperature to two decimals and the light in lux.

Parts list

4 parts, plus the jumper wires. They retail for about $44; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature sensor, TMP117 pin SCLArduino Uno R3 pin SCL
Temperature sensor, TMP117 pin SDAArduino Uno R3 pin SDA
Temperature sensor, TMP117 pin GNDArduino Uno R3 pin GND
Ambient light, VEML7700 pin VINArduino Uno R3 pin 5V
Ambient light, VEML7700 pin GNDArduino Uno R3 pin GND
Ambient light, VEML7700 pin SDAArduino Uno R3 pin A4
Ambient light, VEML7700 pin SCLArduino Uno R3 pin A5
Temperature sensor, TMP117 pin VIN5 V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A TMP117 and a VEML7700 on one I2C bus.
//
// Two sensors, two addresses, one pair of wires: the TMP117 (0x48) reads
// temperature to a hundredth of a degree, the VEML7700 (0x10) reads light in
// lux. I2C lets any number of parts share SDA and SCL as long as each answers
// at its own address.
//
//   TMP117    SDA, SCL -> SDA, SCL (A4, A5)   VIN -> 5V, GND -> GND
//   VEML7700  SDA, SCL -> SDA, SCL (A4, A5)   VIN -> 5V, GND -> GND
//
// Every second the sketch prints both readings on the serial monitor. Move
// the temperature and lux sliders while it runs.
//
// THINGS TO TRY. Wire the TMP117's ADD0 to VIN: it moves to 0x49, begin()
// finds nothing at 0x48, and the sketch says so.

#include <Wire.h>
#include <Adafruit_TMP117.h>
#include <Adafruit_VEML7700.h>

Adafruit_TMP117 tmp117;
Adafruit_VEML7700 veml;

void setup() {
  Serial.begin(9600);
  if (!tmp117.begin()) {
    Serial.println("No TMP117 at 0x48: check SDA, SCL and ADD0.");
    while (1) {}
  }
  if (!veml.begin()) {
    Serial.println("No VEML7700 at 0x10: check SDA and SCL.");
    while (1) {}
  }
  Serial.println("TMP117 at 0x48 and VEML7700 at 0x10 on one bus");
}

void loop() {
  sensors_event_t temp;
  tmp117.getEvent(&temp);
  float lux = veml.readLux();
  Serial.print("temperature ");
  Serial.print(temp.temperature, 2);
  Serial.print(" C, light ");
  Serial.print(lux, 1);
  Serial.println(" lux");
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial at 9600 baud, then calls begin() on each sensor. If the TMP117 or the VEML7700 does not answer, it prints which one is missing and stops in a while (1) loop. If both answer, it prints a confirmation.

void setup() {
  Serial.begin(9600);
  if (!tmp117.begin()) {
    Serial.println("No TMP117 at 0x48: check SDA, SCL and ADD0.");
    while (1) {}
  }
  if (!veml.begin()) {
    Serial.println("No VEML7700 at 0x10: check SDA and SCL.");
    while (1) {}
  }
  Serial.println("TMP117 at 0x48 and VEML7700 at 0x10 on one bus");
}

loop sketch.ino

Asks the TMP117 for an event with getEvent and reads its temperature. It gets the lux from readLux. It prints both on one line, then waits a second.

void loop() {
  sensors_event_t temp;
  tmp117.getEvent(&temp);
  float lux = veml.readLux();
  Serial.print("temperature ");
  Serial.print(temp.temperature, 2);
  Serial.print(" C, light ");
  Serial.print(lux, 1);
  Serial.println(" lux");
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The sketch says No TMP117 at 0x48 This project
    Check SDA, SCL and power. If the ADD0 pin is wired to VIN, the chip moves to 0x49 and the sketch will not find it.
  • Both sensors fail to answer This project
    SDA and SCL may be swapped. SDA goes to A4 and SCL to A5.
  • The sensor is not found. Temperature sensor
    The TMP117 address is 0x48, set by the ADD0 pin, and it can be 0x49, 0x4A or 0x4B. It is a 3.3 V part with I2C, so use level shifting on 5 V boards and add pull-ups.
  • The temperature reads wrong or is slow to settle. Temperature sensor
    Each count is 7.8125 millidegrees C, so multiply the raw 16-bit signed value by 0.0078125. Heat from nearby parts and your fingers changes it. Use averaging and allow time for a conversion.
  • The sensor is not found or the reading is zero. Ambient light
    The I2C address is fixed at 0x10, so only one fits on a bus. Power it at 3.3 V and use pull-ups. The library must enable it, since it can start in shutdown.
  • Lux is clipped, noisy or wrong in bright or dim light. Ambient light
    Set gain and integration time for the light level. Low gain and short integration for sunlight, high gain and long for dim rooms. At the extremes the lux conversion is nonlinear, so use the library's correction.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.