This tutorial was featured on the official Arduino blog on 3/9/2011
As promised, this week brings another communications tutorial! In this video, we’ll use both the serial and processing knowledge that we gained last week, plus an I2C enabled temperature-sensing IC. We’ll use processing to generate a pretty room temperature display on your computer screen. If you are new to processing, they have an excellent set of tutorials to help you get started.
You can download the files associated with this episode here:
Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.













February 16, 2011 at 3:09 pm
thanks! great resource .
Pingback: Must-See Beginner Tutorials For Arduino | inmotion.pt
March 12, 2011 at 1:03 am
I just finished with putting together my first temperature display from this tutorial. I created the background art in Inkscape. I would like to show it to you. What is the best way to do that?
Steven
March 14, 2011 at 10:49 am
Contact me via the contact page link up top.
March 14, 2011 at 2:50 am
How you know it will be 72?
March 14, 2011 at 10:50 am
It’s specified in the datasheet
March 19, 2011 at 9:57 pm
Just how accurate is the I2C temperature sensor at getting room temperature? Are there other temperature sensors? The readout with my build is consistently 70-72°F and the temperature feels much warmer. I can see the temperature change when I pinch the sensor between my fingers.
March 20, 2011 at 2:31 pm
It’s pretty accurate. A thermistor might show fluctuations better, but you’d have to convert the voltage to a temperature on your own.
Pingback: Ellentriek » Blog Archive » Arduino, Processing and sensors Part 2
August 21, 2011 at 10:10 pm
Just tried using an HH10D humidity sensor. I keep getting nothing but zeroes back from the sensor. I used this code:
/*
* Read HH10D humidity sensor (http://www.sparkfun.com/products/10239)
* It contains a M24C02BN6 Eeprom with calibration values.
*
* Author: Patrik Hermansson, http://www.patrikhermansson.se
* Date: 110309
*
* Connections:
* Vss – Gnd
* Vdd – +3.3V
* Fout – D5 (Mega168 pin 11)
* SDA – A4 (pin 27)
* SCL – A5 (pin 28)
*/
#include
int rv = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println(“Hi!”);
}
void loop() {
readCalib(81,10);
Serial.print(“Sensitivity: “);
Serial.println(rv);
readCalib(81,12);
Serial.print(“Offset: “);
Serial.println(rv);
delay(1000);
}
int readCalib( int deviceaddress, int calibAddress ) // deviceaddress is 1 according to datasheet
{
// Set address
Wire.beginTransmission(deviceaddress);
Wire.send(calibAddress);
Wire.endTransmission();
// Request return value
Wire.requestFrom(deviceaddress, 2);
// Collect return value
for (int c = 0; c < 2; c++ )
if (Wire.available()) rv = rv * 256 + Wire.receive();
return rv;
}
Not sure what I am doing wrong. It says the device has an EEPROM and that in order to read out the correct humidity, 4 calibration factors need to be read out from the EEPROM at address of 10 and 11,12 and 13 for sensitivity, offset.
This one blew me away a bit.
September 25, 2011 at 3:42 am
This is a great video and series! Very informative and easy to follow. I have two quick questions;
1) How many of these temperature sensors can you connect to each bus? I’m looking to connect about 10 if possible.
2) If I had multiple sensors on one SDA and SCL line, would I need to connect pull up resistors for each sensor, or would all the sensors share two resistors at the start of the SD and SCL line?
Thanks Sciguy!!
October 28, 2011 at 2:20 pm
You only need one set of pull-ups for the entire bus. You can connect as many as you want so long as they have different IDs.
January 14, 2012 at 9:31 am
Jeremy:
I replicated your schematic using a T 74a7 with an ID of 79 and it worked fine except the temperature reads about 10F higher than it should accross the range frm 20-100F. Is there a means of inserting a calibrating resistor to compensate?
I tried replacing one of the 10k pull up resistor with a variable pot with no change across the 10k range, and in the process found out tha the 10k pull up resistors had no effect at all on the readout.
Would appreciate your comments.
Thanks
January 14, 2012 at 5:52 pm
The pullup won’t affect the value – that’s just for setting the default digital values of the I2C lines. If it is consistently 10 degrees to high, why not just subtract 10 in software from whatever value you are reading?
January 21, 2012 at 2:42 pm
Thanks Jeremy:
I adjusted as follows:
int c=Wire.receive()-5;
Still playing with the correction. But for now -5F seems to work well
Thanks a lot
January 20, 2012 at 7:40 pm
Hi Jeremy,
Great demo.
It’s not the same components, but I have a SRF10 ultra sonic range sensor connected to an Arduino Pro Mini, and I’m trying to get the I2C protocol to work but it’s not happening. Based on my testing (println to check for code progression), I found that when the code gets to the “endTransmission” instruction, it freezes, and nothing else happens. It looks like this has been a problem, but I don’t know how to get around it. Any idea?
Thanks.
January 21, 2012 at 10:47 pm
Have you tried some sample code? http://www.robot-electronics.co.uk/files/srf08.pde
January 26, 2012 at 6:59 pm
Hi Jeremy,
Your tutorials are the best. Thanks for all your effort. As with others above, I’m interested in using more than one sensor on the bus, but am aware that I will need to change the address of the other sensors so that there isn’t a data crash.
How would I
a) write a new address to a device and
b) poll each sensor individually to recover the temperature at each sensor?
I must admit that my biggest difficulty is understanding how to use library functions. I don’t know what each one holds or how to use them correctly.
What resources are there that will explain how to use libraries?
February 13, 2012 at 10:54 am
Most I2C devices cannot have new ID’s assigned to them, so you need to find ones with different ID’s. An alternative is to bit-bang two I2C devices with the same ID on different bus lines.
The best way to understand libraries is to check out the source code and see how the functions are written.
February 7, 2012 at 7:27 pm
Hi,
Very good tutorial. I have two questions, so far:
1. The serial communication to the computer, is in fact an emulation of an RS232 via the USB right ? You are not really doing an RS232 link betwen the Arduino and your PC…
2. If I got it right, the Arduino, here, is a development tool. After you do your programing and testing, would you be able to “burn” a microcontroler in orther to have it embeded in a circuit?
Thank you,
April 23, 2012 at 3:42 pm
Correct, there is chip on the arduino doing RS232 emulation over USB. You can indeed burn the program to a chip built into larger circuit.
February 12, 2012 at 9:08 am
Hello Jeremy!
After your lessons I fill power in my hands and begin to wire different parts to my “clock”
And I was success with LCD (COG 2×16 LCD board from http://www.mikroe.com)
But with I2C RTClock board (PCF8583 chip)I meet some difficulties.
I’ve connected Vss and Ground to the board, pullup with 10kom SDA(5 to A4) & SCL(6 to A5) and …
nothing else. (may be I have to wire 3 & 7 pin on chip to Vcc?)
I was try to read time & date but it is fail.
Help me< where I am wrong?
thank you a lot!
March 3, 2012 at 7:42 am
hi
guys, help me in this error as i m new to arduino
wire.send () has been renamed wire.write().
what does that mean ???
i copied the code and paste it in ardiuno window!!
THANKS
March 21, 2012 at 9:27 am
// New code for Arduino 1.0, library has changed slightly from older versions
//Program by Jeremy Blum
//www.jeremyblum.com
//Reads Temp in Celsius and Shows it on Computer
//Include Wire I2C Library
#include
int temp_address = 72;
void setup()
{
//Create Serial Object (9600 Baud)
Serial.begin(9600);
//Create a Wire Object
Wire.begin();
}
void loop()
{
//Send Request
Wire.beginTransmission(temp_address); //Start talking
Wire.write(8); //Changed command send to write (8 bit device)
Wire.endTransmission(); //Complete Transmission
Wire.requestFrom(temp_address, 1); //Request 1 Byte
while(Wire.available() == 0); //wait for response
int c = Wire.read(); // Changed command from receive to read, Get the temp
//do some math…
int f = round(c*9.0/5.0 +32.0);
Serial.print(c);
Serial.print(“C,”);
Serial.print(f);
Serial.print(“F.”);
delay(500);
}
May 19, 2012 at 12:05 am
I am trying to use PIC18f46j50 as a master and an uno as a slave. I have no problem using the Uno with another Uno. But when I try to use the PIC, even after several approaches to voltage level shifting, etc., I still get no ACK. Has anyone you know ever tried this and is there a protocol trick or other hidden issue here?
July 5, 2012 at 4:32 pm
Hi.
I think I must be using a differnt version of developer to you? I have no font option in my tools?
Mine was recently downloaded from the arduino site.
What developer software are you using?
Bob.
July 5, 2012 at 6:26 pm
I’ve found the proccessing site and downloaded the proccessor, it is similar to the one Iam using but I can’t see any way of uploading programs to the arduino?
what am I missing
Bob
July 10, 2012 at 6:46 am
Hi.!!
I have a sht71 sensor. I’m training to using with arduino but it doesn’t show any number
#include “Sensirion.h”
const uint8_t dataPin = 9;
const uint8_t clockPin = 8;
float temperature;
float humidity;
float dewpoint;
Sensirion tempSensor = Sensirion(dataPin, clockPin);
void setup()
{
Serial.begin(9600);
}
void loop()
{
tempSensor.measure(&temperature, &humidity, &dewpoint);
Serial.print(“Temperature: “);
serialPrintFloat(temperature);
Serial.print(” C, Humidity: “);
serialPrintFloat(humidity);
Serial.print(” %, Dewpoint: “);
serialPrintFloat(dewpoint);
Serial.println(” C”);
delay(3000);
}
void serialPrintFloat(float f){
Serial.print((int)f);
Serial.print(“.”);
int decplace = (f – (int)f) * 100;
Serial.print(abs(decplace));
}
July 10, 2012 at 5:49 pm
Great resources bro.. thanks a lot
August 6, 2012 at 4:20 pm
Hi Jeremy,
Great demo.
It’s not the same components, but I have multiple sensors connected to an Arduino Uno, and I’m trying to get the I2C protocol to work but it’s not happening. Based on my testing (println to check for code progression), I found that when the code gets to the “endTransmission” instruction, it freezes, and nothing else happens. This has been a problem, and I don’t know how to get around it. Here are the links to the data sheets: (http://www.ti.com/lit/ds/symlink/tmp275.pdf) and (http://pdfserv.maxim-ic.com/en/ds/DS1631-DS1731.pdf). Any idea?
Thanks.
August 9, 2012 at 3:06 pm
Hello, Jeremy, Thank you for your work.
When I read the sensor datasheet, it tells me the default address is 1001101(b). when i work that out in binary, I get 77. I don’t know whats up with the “b”. What am I doing wrong? Thank you for your time.
August 14, 2012 at 8:04 am
The b just indicates that it is being written in binary.
September 3, 2012 at 8:56 pm
Thank you, but that begs the question, if the datasheet states the address is 77, why do you declare the “temp_address=72″? in the sketch?
September 3, 2012 at 9:12 pm
It varies based on the part. Mine was 72.
August 20, 2012 at 3:12 pm
Hello.
I am writing the same program as you on Arduino. For some reason, the Wire.requestFrom is freezing the program. I used the Serial.println() to make sure. I used a link that you posted to try to find a way around it. But it still doesn’t work. What do you think I should do??
August 22, 2012 at 10:46 pm
That might mean it can’t communicate with the device. Do you have the ID correct? It could also be a result of which version of the IDE you are using.
September 15, 2012 at 6:12 am
could you please help me to give a processing arduino program the can be using for reading DC/AC voltage from 0-700V, Resistor from 0 Ohm to 2 Mega Ohm and 0-10 Ampere current meter.
October 1, 2012 at 3:36 pm
Hey Jeremy!I have some doubts here.
In the following processing program
void serialEvent (Serial port)
{
data = port.readStringUntil(‘.’);
…..
}
Here “serialEvent” and “readStringUnit” are not marked in orange which means they are not class and method in Processing. So why can they be ran here in the Processing development environment. Also I want to know where are they from?
Thank you for your kind help!!
Waiting for your reply!!
October 1, 2012 at 4:50 pm
You’re importing a Serial class: import processing.serial.*;
October 13, 2012 at 8:54 pm
Thank you Jeremy. I no speak inlgis, but i undtarde your write.
I from Brasil ( Brazil)
Pingback: Tutorial 13 for Arduino: Liquid Crystal Displays | JeremyBlum.com
October 30, 2012 at 11:37 pm
I’ve seen most of your tutorials, and enjoy them all. Haven’t had anything that’s implements i2c until I got myself an RTC from st micro and have been having some issues. My troubles lie, I think, in the clock and data lines. I have pull up resistors in place, but after watching this video again, I noticed what will hopefully be my resolution. You mentioned connecting SDA to A4, and SCL to A5. I’ve been connecting to a couple of pins on the AREF side of my uno (they’re labeled SDA and SCL underneath). Am I not wiring my device to the correct pins? Or is there potentially another issue I should look for?
December 18, 2012 at 4:31 pm
A4 and A5 are multiplexed to acts as SDA and SCL too. It sounds like you are hooking it up correctly.
December 26, 2012 at 1:14 pm
Hi Jeremy,
thank you for the tutorials.
I was fighting also with hte address of the device as i could not find the value in the (online) datasheet.
So i tweaked s.o. sketch to increase the address value until it can read a temperature.
you can see the valid address in the serial Monitor.
#include “Wire.h”
//wire library
#define address 0×01 + l
//address of the temperature sensor
#define delayC 10
//delay count in ms
#define baudrate 9600
//baudrate for communication
int l;
void setup()
{
Wire.begin();
Serial.begin(baudrate);
}
void loop()
{
Serial.print(“temperature in Celsius: “);
//let’s signal we’re about to do something
int temperature;
//temperature in a byte
Wire.beginTransmission(address);
//start the transmission
Wire.write(0×00);
Wire.requestFrom(address, 1);
if (Wire.available()) {
temperature = Wire.read();
Serial.println(temperature);
Serial.print(address);
Serial.println(” address”);
} else {
Serial.println(“—”);
//Serial.print(address);
Serial.println(“Increasing address”);
l++;
}
Wire.endTransmission();
//end the transmission
delay(delayC);
}
Maybe it is usefull to s.o. else.
But i still wonder how you can handle more davices with the sam address on the bus,
as i can not find ‘adjustable’ sensores out there
January 17, 2013 at 1:57 am
Thanks for the videos , can you please do a video of an eeprom maybe AT24C11, ateml I2C eeprom, that only read the content of the eeprom.
My eeprom already have some calibration data, that i am trying to download.
thanks
D
January 17, 2013 at 4:27 am
thanks for the videos dude… i was using an accelerometer…..ADXL345…and the data sheet says ” With the ALT ADDRESS pin high, the 7-bit I2C address for the device is 0x1D, followed by the R/Figure 40Table 11Table 12Figure 41W bit. This translates to 0x3A for a write and 0x3B for a read ” …so how do i set the device address and read and write to it??
thanks in advance
February 12, 2013 at 2:22 pm
I have replicated the circuit exactly on a prototype shield, but when I run the processing code the window pops up with nothing written on it, just a black screen. Can u please help me?
February 14, 2013 at 2:50 am
Do you get any errors in the terminal?
March 4, 2013 at 9:52 am
import processing.serial.*;
Serial port;
int inp = 0;
int wdt=800, ht=600;
void setup()
{
println(Serial.list());
port= new Serial(this,Serial.list()[0],9600);
size(800,600);
}
void serialEvent (Serial port)
{
while(port.available()==0);
inp=port.read();
}
void draw()
{
fill(0,0,0);
text(inp, 400,300);
textSize(25);
}
Please help me the text is over lapping rather than refreshing and its my arduino code
//declare variables
float tempC;
int tempPin = A0;
void setup()
{
pinMode(tempPin,OUTPUT);
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}
void loop()
{
tempC = analogRead(tempPin); //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;//convert the analog data to temperature
int f = round(tempC*9.0/5.0 +32.0);
Serial.print((byte)tempC);//send the data to the computer
Serial.print(“C,”);
Serial.print((byte)f);
Serial.print(“F.”);
delay(500); //wait one second before sending new data
}
Pingback: Tutorials for Arduino Getting Acquainted with Arduino - Arduino for ProjectsArduino for Projects
May 6, 2013 at 7:45 am
It’s a great lesson. Can your help me, how to connect together BMP085 on 3.3V and DS1307 on 5V?
May 13, 2013 at 3:26 pm
thank you! great tutorial. really helpful.