Friday, November 3, 2017

Lab 12 - I2C Bus

CLASSWORK:
TC74 I2C Temperature Sensor Code
//Reads Temp from I2C temperature sensor and prints it on the serial
//port
//Include Wire I2C library
#include <Wire.h>
int temp_address = 73; //1001000 written as decimal number
int speakerPin = 3;
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
 //Create a Wire object
 Wire.begin();
}
void loop()
{
 tone(speakerPin, 1000, 500);
 delay(500);
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");
 delay(500);
}


HOMEWORK:
TC74 Sensor with Proportional Sound Output and LCD Display (Work in Progress)
//Reads Temp from I2C temperature sensor and prints it on the serial port
//Include Wire I2C library
#include <Wire.h>
#include <LiquidCrystal.h>
int temp_address = 73; //1001001 written as decimal number
int speakerPin = 3;
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);
void setup()
{
 //Start serial communication at 9600 baud
Serial.begin(9600);
Serial.print("Startting..");
 //Create a Wire object
 Wire.begin();
 lcd.begin(16,2);
 //lcd.print("TEMP DISPLAY:");
 //lcd.setCursor(0,1);
 //lcd.print("Please print!");
}
void loop()
{
 tone(speakerPin, 1000, 500);
 delay(500);
 //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0)
 Serial.println("loading..");
 //Get the temp and read it into a variable
 int c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 int f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.println("F");

 //Send the temperature in degrees C and F to the LCD display
 lcd.setCursor(2,1);
 lcd.print(c);
 lcd.setCursor(3,1);
 lcd.print("C");
 lcd.setCursor(7,1);
 lcd.print(f);
 lcd.setCursor(8,1);
 lcd.print("F");

 delay(500);

}

No comments:

Post a Comment