Friday, 31 May 2019

temperature-monitor-using-msp430-launchpad-and-lm35-temperature-sensor

Here I summarize the hardware connections and source code to build a Temperature Monitor using MSP430 Launchpad and LM35 Sensor.
Project: Temperature Monitor with LCD Display
Microcontroller: MSP430G2231 on MSP-EXP430G2 Launchpad
Temperature Sensor: LM35
16×2 LCD Display: 1602K27-00

Hardware connections

Board jumper changes:
1. Isolate LEDs connected to P1.0 and P1.6 by removing Jumpers cap J5.
2. Isolate RX/TX connected to P1.1 and P1.2 by removing those Jumper cap in J3
Microcontroller and Temperature sensor Connections:
P1.1 – Vout of LM35
Microcontroller and LCD Connections
TP1 – Vcc (+5v)
TP3 – Vss (Gnd)
P1.2 – EN
P1.3 – RS
P1.4 – D4
P1.5 – D5
P1.6 – D6
P1.7 – D7
Gnd – RW
Gnd – Vee/Vdd – Connect to Gnd through a 1K Resistor – this value determines contrast – i.e. without resistor all dots always visible,  whereas higher resistor means dots not at all displayed.
Gnd – K (LED-)
Vcc – A (LED+) +5V – For Backlight
Clock: 1MHz
 Usage of LM35

Usage of LM35
Temperature Monitor Schematic
Temperature Monitor Schematic

This project is built using Energia IDE with integrated build tool chain (compiler/linker).

Source code

#include <msp430g2231.h>
#include <stdlib.h>
#include <string.h>
// uC GPIO Port assignment
#define UC_PORT      P1OUT
#define UC_PORT_DIR P1DIR
// LCD pin assignments
#define LCD_EN        BIT2
#define LCD_RS        BIT3
#define LCD_DATA    BIT4 | BIT5 | BIT6 | BIT7
#define LCD_D0_OFFSET 4 // D0 at BIT4, so it is 4
#define LCD_MASK    LCD_EN | LCD_RS | LCD_DATA
// Connect P1.1 to LM35 temperature sensor output
#define TEMP_IN      BIT1
char temperature_string[4];
short temperature = 0;
void lcd_reset()
{
UC_PORT = 0x00;
__delay_cycles(20000);
UC_PORT = (0x03 << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
__delay_cycles(10000);
UC_PORT = (0x03 << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
__delay_cycles(1000);
UC_PORT = (0x03 << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
__delay_cycles(1000);
UC_PORT = (0x02 << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
__delay_cycles(1000);
}
void lcd_cmd (char cmd)
{
// Send upper nibble
UC_PORT = (((cmd >> 4) & 0x0F) << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
// Send lower nibble
UC_PORT = ((cmd & 0x0F) << LCD_D0_OFFSET) | LCD_EN;
UC_PORT &= ~LCD_EN;
__delay_cycles(4000);
}
void lcd_data (unsigned char dat)
{
// Send upper nibble
UC_PORT = ((((dat >> 4) & 0x0F) << LCD_D0_OFFSET) | LCD_EN | LCD_RS);
UC_PORT &= ~LCD_EN;
// Send lower nibble
UC_PORT = (((dat & 0x0F) << LCD_D0_OFFSET) | LCD_EN | LCD_RS);
UC_PORT &= ~LCD_EN;
__delay_cycles(4000); // a small delay may result in missing char display
}
void lcd_init ()
{
UC_PORT_DIR = LCD_MASK;     // Output direction for LCD connections
lcd_reset();         // Call LCD reset
lcd_cmd(0x28);       // 4-bit mode – 2 line – 5×7 font.
lcd_cmd(0x0C);       // Display no cursor – no blink.
lcd_cmd(0x06);       // Automatic Increment – No Display shift.
lcd_cmd(0x80);       // Address DDRAM with 0 offset 80h.
lcd_cmd(0x01);     // Clear screen
}
void display_line(char *line)
{
while (*line)
lcd_data(*line++);
}
void display_temperature(char *line, int len)
{
while ((3-len) > 0)
lcd_data(‘ ‘);
while (len–) {
if (*line)
lcd_data(*line++);
}
lcd_data(0xDF); // degree symbol
lcd_data(‘C’);
}
void initADC(void)
{
// initialize 10-bit ADC
UC_PORT_DIR &= ~TEMP_IN;  // input direction for output from sensor
ADC10CTL0 |= ADC10ON;
ADC10CTL1 |= INCH_1|ADC10SSEL_1|CONSEQ_1;
ADC10AE0  |= BIT0 | BIT1;
ADC10CTL0 |= ENC|ADC10SC;
}
void setup() {
WDTCTL = WDTPW + WDTHOLD;   // Stop Watch Dog Timer
// Initialize LCD
lcd_init();
// Initialize ADC
initADC();
lcd_cmd(0x80); // select 1st line (0x80 + addr) – here addr = 0x00
display_line(“Temperature”);
lcd_cmd(0xce); // select 2nd line (0x80 + addr) – here addr = 0x4e
}
void loop() {
// measuring the temperature
temperature = (analogRead(A1)*35)/100;
// displaying the current temperature
lcd_cmd(0xcb); // select 2nd line (0x80 + addr) – here addr = 0x4b
itoa(temperature, temperature_string, 10);
display_temperature(temperature_string, 3);
__delay_cycles(500000);       // 0.5sec measurement cycle
}
You can also download source code
  Temperature_Monitor.ino (3.1 KiB, 1,076 hits)
Temperature_Monitor.ino - this is basically a C source file in Energia IDE format.
Temperature Monitor in action

Temperature Monitor in action

No comments: