Electronic project demo by Lewis Loflin

Arduino Reads Temperature Sensor Displays Temperature on LCD Display

by Lewis Loflin

  
  

This is another variation of two following programs combined into one. This code has been tested, the TMP37 sensor has been connected to A0.

Electrical connections ADS1115.
Electrical connections ADS1115.



Arduino sketch for this project: lcd_i2c_ads1115.ino


/*

 By Lewis Loflin lewis@bvu.net
 http://www.sullivan-county.com/main.htm
 Electronics website:
 http://www.bristolwatch.com/index.htm

 Arduino uses ADS1115 4-channel 15-bit
 ADC to read TMP37 Centigrade temp sensor.
 The sensor voltage and temperature in
 Fahrenheit is displayed on I2C based
 LCD display.

 */

// Define some device parameters
#define I2C_ADDR   0x27 // I2C device address

// Define some device constants
#define LCD_CHR  1 // Mode - Sending data
#define LCD_CMD  0 // Mode - Sending command

#define LINE1  0x80 // 1st line
#define LINE2  0xC0 // 2nd line

#define LCD_BACKLIGHT   0x08  // On
// LCD_BACKLIGHT = 0x00  # Off

#define ENABLE  0b00000100 // Enable bit

#include <Wire.h>

void lcd_init(void);
void lcd_byte(int bits, int mode);


// added by Lewis
void typeInt(int i);
void typeFloat(float myFloat);
void lcdLoc(int line); //move cursor
void ClrLcd(void); // clr LCD return home
void typeln(const char *s);

char array1[] = "Hello world!";

#define ASD1115  0x48
unsigned int val = 0;

byte writeBuf[3];
byte buffer[3];

const float VPS = 4.096 / 32768.0; // volts per step

void setup(void) {

  Wire.begin(); // begin I2C

  // ASD1115
  // set config register and start conversion
  // AIN0 and GND, 4.096v, 128s/s
  // Refer to page 19 area of spec sheet
  writeBuf[0] = 1; // config register is 1
  writeBuf[1] = 0b11000010; // 0xC2 single shot off
  // bit 15 flag bit for single shot not used here
  // Bits 14-12 input selection:
  // 100 ANC0; 101 ANC1; 110 ANC2; 111 ANC3
  // Bits 11-9 Amp gain. Default to 010 here 001 P19
  // Bit 8 Operational mode of the ADS1115.
  // 0 : Continuous conversion mode
  // 1 : Power-down single-shot mode (default)

  writeBuf[2] = 0b10000101; // bits 7-0  0x85
  // Bits 7-5 data rate default to 100 for 128SPS
  // Bits 4-0  comparator functions see spec sheet.

  // setup ADS1115
  Wire.beginTransmission(ASD1115);  // ADC
  Wire.write(writeBuf[0]);
  Wire.write(writeBuf[1]);
  Wire.write(writeBuf[2]);
  Wire.endTransmission();

  delay(500);

}

void loop(void) {
  // read sensor
  buffer[0] = 0; // pointer
  Wire.beginTransmission(ASD1115);  // DAC
  Wire.write(buffer[0]);  // pointer
  Wire.endTransmission();

  Wire.requestFrom(ASD1115, 2);
  buffer[1] = Wire.read();  //
  buffer[2] = Wire.read();  //
  Wire.endTransmission();

  // convert display results
  val = buffer[1] << 8 | buffer[2];

  if (val > 32768) val = 0;
  // volts per step
  float myfloat = val * VPS; // convert to voltage

  // display sensor voltage
  ClrLcd(); // defaults LINE1
  typeln("V = ");
  typeFloat(myfloat);

  // display Temperature F
  lcdLoc(LINE2);
  typeln("Deg. F = ");
  typeFloat((myfloat / 0.02 ) * 9 / 5 + 32);

  delay(500);
} // end loop do it again

//  float to char string
void typeFloat(float myFloat)   {
  char buffer[20];
  // ftoa() replacement
  dtostrf(myFloat, 5, 3, buffer);
  typeln(buffer);
}

// int to char string
void typeInt(int i)   {
  char array1[20];
  sprintf(array1, "%d",  i);
  typeln(array1);
}

// clr lcd go home loc 0x80
void ClrLcd(void)   {
  lcd_byte(0x01, LCD_CMD);
  lcd_byte(0x02, LCD_CMD);
}

// go to location on LCD
void lcdLoc(int line)   {
  lcd_byte(line, LCD_CMD);
}

// out char to LCD at current position
void typeChar(char val)   {
  lcd_byte(val, LCD_CHR);
}

// this allows use of any size string
void typeln(const char *s)   {
  while ( *s ) lcd_byte(*(s++), LCD_CHR);
}

void lcd_byte(int bits, int mode)   {

  //Send byte to data pins
  // bits = the data
  // mode = 1 for data, 0 for command
  byte bits_high, bits_low;

  // uses the two half byte writes to LCD
  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT ;
  bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT ;

  Wire.beginTransmission(I2C_ADDR); // address device

  // High bits
  Wire.write(bits_high);
  // Toggle enable pin on LCD display
  delayMicroseconds(100);
  Wire.write(bits_high | ENABLE);
  delayMicroseconds(100);
  Wire.write(bits_high & ~ENABLE);
  delayMicroseconds(100);

  Wire.write(bits_low);
  // Toggle enable pin on LCD display
  delayMicroseconds(500);
  Wire.write(bits_low | ENABLE);
  delayMicroseconds(500);
  Wire.write(bits_low & ~ENABLE);
  delayMicroseconds(500);

  Wire.endTransmission();
}

void lcd_init()   {
  // Initialise display
  lcd_byte(0x33, LCD_CMD); // Initialise
  lcd_byte(0x32, LCD_CMD); // Initialise
  lcd_byte(0x06, LCD_CMD); // Cursor move direction
  lcd_byte(0x0C, LCD_CMD); // 0x0F cursor on  Display On,Cursor On, Blink Off
  lcd_byte(0x28, LCD_CMD); // Data length, number of lines, font size
  lcd_byte(0x01, LCD_CMD); // Clear display
  delayMicroseconds(500);
}

For earlier Arduino Projects see Arduino Projects Hobby Projects Tutorials




Videos:
My YouTube Videos on Electronics
Introduction to the Arduino Microcontroller
Part 1: Programming Arduino Output
Part 2: Programming Arduino Input
Part 3: Arduino Analog to Digital Conversion
Part 4: Using Arduino Pulse-Width-Modulation
Repost Arduino AC Power Control

arduino

See How I got into Electronics

New May 1, 2016:

I'd say my job title would be applied technologist. See Electronics Projects for Students and Hobbyists

Recent YouTube Videos: