Categories

Am2301 and LCD Arduino example

The AM2301  is a wired version of the DHT21, in a large plastic body.

AM2301

AM2301

Specifications:

  • Type: AM2301
  • Accuracy resolution: 0.1
  • Measurement range: 0-100%RH
  • Temperature measurement range: -40℃ ~ +80℃
  • Humidity measurement precision: ±3%RH
  • Temperature measurement precision: ±0.5℃

This Sensor has 3 wires, connect these wires to your Arduino like this

Sensor Arduino
Red +5V
Black GND
Yellow Digital I/O

Code

Since this is basically a packaged up DHT21, you use DHT libraries and here is a code example that will display the temperature and humidity readings on an LCD display.

#include "DHT.h"
#include <LiquidCrystal.h>
 
#define DHTPIN 11 // modify to the pin we connected
#define DHTTYPE DHT21 // AM2301 
DHT dht(DHTPIN, DHTTYPE);
 
//setup for the LCD keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
void setup() 
{
 Serial.begin(9600); 
 lcd.begin(16,2);
 Serial.println("DHTxx test!");
 dht.begin();
 //line 1 - Temperature
 lcd.setCursor(0,0);
 lcd.print("Temp: ");
 //line 2 - Humidity
 lcd.setCursor(0,1);
 lcd.print("Hum: ");
}
 
void loop() 
{
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 // check if returns are valid, if they are NaN (not a number) then something went wrong!
 if (isnan(t) || isnan(h)) 
 {
 Serial.println("Failed to read from DHT");
 } 
 else 
 {
 lcd.setCursor(7,0); 
 lcd.print(h);
 lcd.setCursor(7,1); 
 lcd.print(t); 
 delay(2000);
 }
}

Links
AM2301 DHT21 Capacitance Digital Temperature&Humidity Sensor

MCP4725 and LCD example

MCP4725 is a single channel, 12-bit, voltage output Digital-to-Analog Converter with integrated EEPROM and an I2C Compatible Serial Interface.

mcp4725

mcp4725

Features

12-Bit Resolution
On-Board Non-Volatile Memory (EEPROM)
±0.2 LSB DNL (typ)
External A0 Address Pin
Normal or Power-Down Mode
Fast Settling Time of 6µs (typ)
External Voltage Reference (VDD)
Rail-to-Rail Output
Low Power Consumption
Single-Supply Operation: 2.7V to 5.5V
I2CTM Interface:
Eight Available Addresses
Standard (100 kbps), Fast (400 kbps) andHigh Speed (3.4 Mbps) Modes
Small 6-lead SOT-23 Package
Extended Temperature Range: -40°C to +125°C

As this is a 12 bit DAC converter. What this means is that it will accept up to 4096 possible inputs to provide an analog output, where an output value of zero is zero and an output value of 4095 is full scale.
Full scale is determined by the reference voltage you supply to the VCC pin. Also you can see from above that the supply voltage can be anywhere from 2.7 volts to 5.5 volts. We will use 5v, or as close as what is supplied via the USB in. You may want to measure this voltage for accurate readings, I’ve seen this vary.

This means that to work out the value of the Least Significant Bit (LSB) is as follows:

1 LSB = VCC Voltage / 4096
Again the easiest way to interface this to an Arduino is to purchase a module, tehse are available from many sources, here is what my one looked at.

The module has the ability to use different I2C addresses, so run the code from here first.  My one was the default 0x60 address

 

Schematic

This uses an I2C connection and the VOUT/OUT from the module connects to A1 in this example.

 

mcp4725 schematic

mcp4725 schematic

Code

This code example uses the Adafruit library, in the most recent (1.6.5 onwards) Arduino IDE you can import this using Include Libraries -> Manage Libraries. Basically we increment the DAC, read in the value and display the results on an LCD shield

#include <Wire.h>
#include <Adafruit_MCP4725.h>
#include <LiquidCrystal.h>
 
 
#define MCP4725In A1
Adafruit_MCP4725 MCP4725;
//setup for the LCD keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
void setup() 
{
  // put your setup code here, to run once:
  delay(1000);
  lcd.begin(16,2);
  MCP4725.begin(0x60); // The I2C Address of my module 
  //line 1 - Expected reading
  lcd.setCursor(0,0);
  lcd.print("Exp: ");
  //line 2 - Actual reading
  lcd.setCursor(0,1);
  lcd.print("Act: ");
 
}
 
void loop() 
{
  // put your main code here, to run repeatedly:
  uint32_t MCP4725_value;
  int adcInput = 0;
  float voltageIn = 0; 
  float MCP4725_reading;
  for (MCP4725_value = 0; MCP4725_value < 4096; MCP4725_value = MCP4725_value + 128)
  {
    delay(1000);
    MCP4725_reading = (5.0/4096.0) * MCP4725_value; //5.0 is your supply voltage
    MCP4725.setVoltage(MCP4725_value, false);
    adcInput = analogRead(MCP4725In); //module output connect to A0
    voltageIn = (adcInput * 5.0 )/ 1024.0; 
    lcd.setCursor(7,0); 
    lcd.print(MCP4725_reading,3);
    lcd.setCursor(7,1);     
    lcd.print(voltageIn,3);      
  }    
 
}

 

Links

MCP4725 I2C DAC Breakout Development Board 12Bit Resolution

 

 

HCT1008 sensor and LCD example for Arduino

In this example we will show the temperature and humidity readings from a HDC100x series sensor and display this on a 16×2 LCD.

First lets take a look at the sensor

The HDC1000 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The device measures humidity based on a novel capacitive sensor. The humidity and temperature sensors are factory calibrated.

The sensing element of the HDC1000 is placed on the bottom part of the device, which makes the HDC1000 more robust against dirt, dust, and other environmental contaminants. The HDC1000 is functional within the full –40°C to +125°C temperature range.

Key Features

  • Relative Humidity (RH) Operating Range 0% to 100%
  • 14 Bit Measurement Resolution
  • Relative Humidity Accuracy ±3%
  • Temperature Accuracy ±0.2°C
  • Supply Voltage 3 V to 5 V
  • I2C Interface

Connection information

The easiest way is to purchase some sort of breakout like the one pictured earlier

Connect Vin to 5VDC
Connect GND to ground
Connect SCL to A5 on UNO
Connect SDA to A4 on UNO

I used the headers on the LCD keypad shield, here is a picture of this

lcd keypad shield

lcd keypad shield

Parts

1 x Arduino Uno
1 x LCD Keypad shield
1 x HDC1008 breakout
Dupont hookup cable x 4

Code

The good folks at Adafruit have done the hard work and produced a library for this device – https://github.com/adafruit/Adafruit_HDC1000_Library  . If you are using Arduino IDE 1.6.5 or later you can easily import this library by going to Sketch -> Include Library -> Manage Libraries

#include <Wire.h>
#include "Adafruit_HDC1000.h"
#include <LiquidCrystal.h>
 
//setup for the LCD keypad shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// Connect Vin to 5VDC
// Connect GND to ground
// Connect SCL to A5 on UNO
// Connect SDA to A4 on UNO
 
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
 
void setup() 
{
  Serial.begin(9600);
  Serial.println("HDC100x test");
  //lcd setup
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  //line 2 - humidity
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  if (!hdc.begin()) 
  {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}
 
 
void loop() 
{
  Serial.print("Temp: "); Serial.print(hdc.readTemperature());
  Serial.print("\t\tHum: "); Serial.println(hdc.readHumidity());
  lcd.setCursor(7,0);
  lcd.print(hdc.readTemperature());
  lcd.setCursor(11,1);
  lcd.print(hdc.readHumidity());
  delay(500);
}

 

Links

 

Adafruit HDC1008 Temperature & Humidity Sensor Breakout Board
1602 LCD Keypad Shield for Arduino

ST Nucleo F334R8 LCD example

In this example we will add an Arduino LCD shield to a ST Nucleo F334R8. This is a 16×2 LCD Keypad module for Arduino Diecimila Duemilanove, UNO, MEGA1280, MEGA2.

Here is a picture of this shield

lcd keypad shield

lcd keypad shield

Code

The code is for the mBed online compiler at https://developer.mbed.org/ and uses the freetronics LCD library

#include "mbed.h"
#include "freetronicsLCDShield.h"
 
freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D7, D1, A0);
 
 
int main() 
{
    lcd.cls();
    // print the first line and wait 3 sec
    lcd.printf("LCD test");
    wait(3);
 
    // print the counter prefix; the number will be printed in the while loop
    lcd.setCursorPosition(1, 0);
    lcd.printf("seconds : ");
 
    int i=1;
    while (i++) 
    {
        lcd.setCursorPosition(1, 11);
        lcd.printf("%d", i);
        wait(1);
    }
}

mBed and 16×2 LCD

This is made easy by the TEXT LCD library. The schematic shows how to connect up the 16×2 LCD to the MBEd

Schematic

mbed and lcd

mbed and lcd

Code


#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7

int main() {
lcd.printf("Hello World!\n");
}