Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save john212/33001ad17c3d58d9c1de to your computer and use it in GitHub Desktop.
Save john212/33001ad17c3d58d9c1de to your computer and use it in GitHub Desktop.
Multiple I2C Devices With Arduino Uno
/**************************************************
Udemy Arduino Step-by-Step Course
Section 4 Displays Lecture 37 to 39
Using 16x2 LCD With I2C Plus a Real Time Clock
by J. Cantlin
January 23, 2016
***************************************************/
/**************************************************
Summary of Arduino Analog Pins Used:
A0 = 4x4 Keypad
A1 = LDR Light Dependent Resistor
A2 =
A3 =
A4 = 16x2 Serial I2C Board SCL (clock) Pin
A5 = 16x2 Serial I2C Board SDA (data) Pin
***************************************************/
/**************************************************
Summary of Arduino Digital Pins Used:
00 =
01 =
02 = DHT11 Temperature-Humidity Sensor
03 =
04 =
05 =
06 =
07 = Pin 7 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 4 RS (Register Select)
08 = Pin 8 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 6 EN (Enable Signal)
09 = Pin 7 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 11 DB4 (Data Bus Line 4)
10 = Pin 7 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 12 DB4 (Data Bus Line 5)
11 = Pin 7 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 13 DB4 (Data Bus Line 6)
13 = Pin 7 Free Due to 16x2 Serial I2C Board - was 1602 LCD Pin 14 DB4 (Data Bus Line 7)
***************************************************/
/**************************************************
DHT11 Temperature-Humidy Sensor
Uses library DHT.h
Must define DHT sensor type, DHT11, DHT21, DHT22 also.
Call the library using DHT dht(pin_used,dht_sensor_type).
External Pull Up Resistor:
The DHT11 needs a 5k ohm pullup resistor, connect it
between the 5v power and the signal line. The
Arduino has internal pull-ups which could be used,
but their exact value in unknown. (10k to 40k?) so
use the known 5k external pull up resistor instead.
***************************************************/
#include "DHT.h"
#define DHTPIN 2 //analog pin DHT sensor uses
#define DHTTYPE DHT11 //type of sensor
DHT dht(DHTPIN, DHTTYPE); //call DHT library
/**************************************************/
/***************************************************
1602 LCD Display - 16 Characters-Two Lines - 16 Pins
This sketch is used with an Adafruit P-tec P-1602-17 16x2 LCD.
and is compatible with the Hitachi HD44780 driver.
Summary of 1602 Pins Used:
01 = Ground (-)
02 = 5vdc (+)
03 = Contrast - wiper arm of 10k ohm pot (5vdc)
04 = RS (Register Select)
05 = GROUND (no display unless pin 5 grounded)
06 = EN or E (Enable Signal)
07 =
08 =
09 =
10 =
11 = DB4 (Data Bus Line 4)
12 = DB5 (Data Bus Line 5)
13 = DB6 (Data Bus Line 6)
14 = DB7 (Data Bus Line 7)
15 = Backlight Power (+ 5vdc)
16 = Backlight Ground (-)
If the LiquidCrystal Library is used to directly drive the LCD (i.e. not I2C)
Use the following libraries and initalization. This is an Adafruit Library.
#include <LiquidCrystal.h> //LCD library for 16x2 text LCD
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //define Arduino pins used with LCD
***************************************************/
/**************************************************
This sketch uses the FC-113 serial board that has 16 mating pins to
connect to the 16x2 LCD and then only two pins from it are connected to
the Arduino to implement I2C serial communication between the LCD
and the Arduino.
Replace "include LiquidCrystal.h", with "include LiquidCrystal_I2C.h"
and "include LCD.h". Also call "include Wire.h" for I2C functions.
The LiquidCrystal.h Library needed for I2C is on BitBucket at:
https://bitbucket.org/fmalpartida/new-liquidcrystal/src
It supports most Hitachi HD44780 based LCDs, or compatible,
connected to any project using: 4, 8 wire parallel interface,
I2C IO port expander (native I2C and bit bang) and Shift Regiter.
It currently supports the following types of connections:
* 4 bit parallel LCD interface
* 8 bit parallel LCD interface
* I2C IO bus expansion board with the PCF8574* I2C IO expander ASIC such as I2C LCD extra IO.
* ShiftRegister adaptor board as described Shift Register project home or in the HW configuration described below, 2 and 3 wire configurations supported.
* ShiftRegister 3 wire latch adaptor board as described ShiftRegister 3 Wire Home
* Support for 1 wire shift register ShiftRegister 1 Wire
* I2C bus expansion using general purpose IO lines.
Initalize the LCD parameters:
lcd(I2C address,*serial board and Arduino pin setup*,BACKLIGHT POLARITY)
Constructor with backlight control:
(lcd_Addr, Enable, ReadWrite, ReSet, d4, d5, d6, d7, backlighPin, backlighPol)
The serial device unique 0x27 address can be changed via solder pads of the FC-113 serial board.
For information on how to determine (via a sketch) any devices' I2C address:
http://todbot.com/blog/2009/11/29/i2cscanner-pde-arduino-as-i2c-bus-scanner/
************************************************ */
/***************************************************/
#include <Wire.h> //native I2C library
#include <LCD.h> //sub library of LiquidCrystal library
#include <LiquidCrystal_I2C.h> //sub library of LiquidCrystal library
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE); //setup serial board
/**************************************************/
/********************************************************
This sketch also uses a Real Time Clock based on the DS3231N IC by Maxim.
Libraries needed
#include <DS1307RTC.h> //For DS3231 also, Ref: github.com/PaulStoffregen/DS1307RTC
#include <Time.h> //Ref: github.com/PaulStoffregen/Time
#include <Wire.h> //native I2C library
*******************************************************/
#include <Time.h> //RTC functions
#include <DS1307RTC.h> //For DS3231 RTC also
//define the RTC related variables using a RTC function
tmElements_t tm; //tm will store the as read RTC data
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/**************************************************/
/****Begin Main Program****/
//define temperature and humidy ralated variables
float h = 0.0;
float t = 0.0;
float t_degF = 0.0;
//define LDR - light dependent resistor ralated variables
int ldr = 0;
//define keypad related variables
char* keypressed = 0; //the * is a pointer.
int keyboardPin = 0; // Analog input pin that the keypad is attached to
int keyboardValue = 0; // value read from the keyboard
void setup()
{//***start setup function****
dht.begin(); //start dht function.
lcd.begin(16, 2); //start LCD with 16 colums 2 rows
Serial.begin(9600); //hardware serial to PC
delay(100); //allow time to startup and stabilize
lcd.backlight(); //turn backlight on (default)
lcd.setCursor(0,0); //move cursor to start position
lcd.print("SBS Quiz"); //print initial message to LCD
delay(100); //initial delay in millisec to read display, let circuit settle.
//Use time program was compiled to set the RTC
// get the date and time the compiler was run
bool parse=false;
bool config=false;
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
}//***endof setup function****
void loop()
{//***start infinite loop****
keyboardValue = analogRead(A0); // read the keyboard value (0 - 1023)
//do nothing until a key is pressed
while (keyboardValue < 25)
{//****start while no key pressed loop****
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t_degF = 1.8*t+32.0;
ldr = analogRead(A1);
keyboardValue = analogRead(A0);
delay(50);
}//****endof while no key pressed loop****
//when a key is pressed get its value
readkeyboard(); //get the value of key being pressed "keypressed" i.e. 0-9
}//***endof infinite loop****
void readkeyboard()
{//***start read keyboard function****
keyboardValue = analogRead((A0)); // read the value (0-1023)
Serial.print("Pin value:");
Serial.println( keyboardValue);
if (keyboardValue <25){keypressed = "n";} //measured 0.0v
if ((keyboardValue >870) && (keyboardValue < 878))
{
keypressed = "1"; //Keypad 1 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Temp. deg F"); //print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(t_degF,DEC); //DEC means base ten decimal
delay(200);
}
if ((keyboardValue >763) && (keyboardValue < 771))
{
keypressed = "2"; //Keypad 2 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Humidity %"); //print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(h,DEC); //DEC means base ten decimal
delay(200);
}
if ((keyboardValue >666) && (keyboardValue < 670))
{keypressed = "3"; //Keypad 3 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Light Intensity");//print title on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(ldr,DEC); //DEC means base ten decimal
delay(200);
}
//if ((keyboardValue >607) && (keyboardValue < 611)){keypressed = "A";}
//if ((keyboardValue >528) && (keyboardValue < 532)){keypressed = "4";}
//if ((keyboardValue >487) && (keyboardValue < 491)){keypressed = "5";}
//if ((keyboardValue >445) && (keyboardValue < 449)){keypressed = "6";}
//if ((keyboardValue >418) && (keyboardValue < 422)){keypressed = "B";}
//if ((keyboardValue >358) && (keyboardValue < 362)){keypressed = "7";}
if ((keyboardValue >337) && (keyboardValue < 345))
{
keypressed = "8"; //Keypad 8 pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Hello from"); //print message on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print("John Cantlin"); //print message on first row
delay(200);
}
//if ((keyboardValue >318) && (keyboardValue < 322)){keypressed = "9";}
//if ((keyboardValue >304) && (keyboardValue < 308)){keypressed = "C";}
if ((keyboardValue >304) && (keyboardValue < 308))
{
keypressed = "C"; //Keypad C pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Time"); //print message on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(__TIME__);
delay(200);
}
//if ((keyboardValue >276) && (keyboardValue < 280)){keypressed = "*";}
//if ((keyboardValue >265) && (keyboardValue < 269)){keypressed = "0";}
//if ((keyboardValue >251) && (keyboardValue < 255)){keypressed = "#";}
if ((keyboardValue >240) && (keyboardValue < 249))
{
keypressed = "D"; //Keypad D pressed
lcd.clear(); //clear LCD
lcd.setCursor(0, 0); //move cursor to start of first row
lcd.print("Date"); //print message on first row
lcd.setCursor(0, 1); //move cursor to start of second row.
lcd.print(__DATE__);
delay(200);
}
//if (keyboardValue >950){keypressed = "e";}
while (keyboardValue > 25)
{
delay (100);
keyboardValue = analogRead((A0)); // read the value (0-1023)
}//wait until key no longer being pressed before continuing
Serial.println(keypressed); // print the value back to the Serial view window on your PC
delay(500); // delay milliseconds before the next loop
}//***endof read keyboard function****
//RTC Functions
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment