Categories

Arduino and SHT11 example

The SHT1x digital humidity sensor is a reflow solderable sensor. The SHT1x series consists of a low-cost version with the SHT10 humidity sensor, a standard version with the SHT11 humidity sensor, and a high-end version with the SHT15 humidity sensor. As with every other Sensirion sensor type from the SHTxx humidity sensor family, they are fully calibrated and provide a digital output.

The humidity sensors are seamlessly coupled to a 14-bit-analog-to-digital converter and a serial interface circuit. This results in superior signal quality, a fast response time, and insensitivity to external disturbances (EMC).

One thing to note is that these sensors have been effectively replaced by others in sensirion’s range such as the SHT31 but they still work and have good performance for hobbyists

 

Layout

 

Code

You need to install the library from – https://github.com/practicalarduino/SHT1x

This is the built in example

#include <SHT1x.h>
 
// Specify data and clock connections and instantiate SHT1x object
#define dataPin  10
#define clockPin 11
SHT1x sht1x(dataPin, clockPin);
 
void setup()
{
   Serial.begin(38400); // Open serial connection to report values to host
   Serial.println("Starting up");
}
 
void loop()
{
  float temp_c;
  float temp_f;
  float humidity;
 
  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();
 
  // Print the values to the serial port
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("C / ");
  Serial.print(temp_f, DEC);
  Serial.print("F. Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
 
  delay(2000);
}

 

Testing

Open the serial monitor , all going well you should see something like this

Starting up
Temperature: 21.9199981689C / 71.5639953613F. Humidity: 47.88%
Temperature: 21.9499969482C / 71.5999984741F. Humidity: 47.88%
Temperature: 21.9499969482C / 71.5819931030F. Humidity: 47.85%

 

Links
SHT11 Digital Temperature and Humidity Sensor,Single bus output temperature and humidity module

Share

Arduino and PCF8563 RTC example

The PCF8563 is a CMOS Real-Time Clock (RTC) and calendar optimized for low power consumption. A programmable clock output, interrupt output, and voltage-low detector are also provided. All addresses and data are transferred serially via a two-line bidirectional I²C-bus. Maximum bus speed is 400 kbit/s.

This is an easy to use module for this device

Features

  • Provides year, month, day, weekday, hours, minutes, and seconds based on a 32.768 kHz quartz crystal
  • Century flag
  • Clock operating voltage: 1.0 V to 5.5 V at room temperature
  • Low backup current; typical 0.25 μA at VDD = 3.0 V and Tamb = 25 °C
  • 400 kHz two-wire I²C-bus interface (at VDD = 1.8 V to 5.5 V)
  • Programmable clock output for peripheral devices (32.768 kHz, 1.024 kHz, 32 Hz, and 1 Hz)
  • Alarm and timer functions
  • Integrated oscillator capacitor
  • Internal Power-On Reset (POR)
  • I²C-bus slave address: read A3h and write A2h
  • Open-drain interrupt pin

 

This is the schematic for a typical module

pcf8563 schematic

pcf8563 schematic

Here is how to connect the module to your Arduino

Connection

Arduino Pin Module Pin
 5v  Vcc
GND Gnd
A5 SCL
A4 SDA

 

Code

You do not need a library but I downloaded the library from https://bitbucket.org/orbitalair/arduino_rtc_pcf8563/downloads

This is one of the basic examples

/* Demonstration of Rtc_Pcf8563 Alarms. 
 *
 * The Pcf8563 has an interrupt output, Pin3.
 * Pull Pin3 HIGH with a resistor, I used a 10kohm to 5v.
 * I used a RBBB with Arduino IDE, the pins are mapped a 
 * bit differently.  Change for your hw.
 * SCK - A5, SDA - A4, INT - D3/INT1
 *
 * After loading and starting the sketch, use the serial monitor
 * to see the clock output.
 * 
 * setup:  see Pcf8563 data sheet.
 *         1x 10Kohm pullup on Pin3 INT
 *         No pullups on Pin5 or Pin6 (I2C internals used)
 *         1x 0.1pf on power
 *         1x 32khz chrystal
 *
 * Joe Robertson, jmr
 * orbitalair@bellsouth.net
 */
#include <Wire.h>
#include <Rtc_Pcf8563.h>
 
/* get a real time clock object */
Rtc_Pcf8563 rtc;
/* a flag for the interrupt */
volatile int alarm_flag=0;
 
/* the interrupt service routine */
void blink()
{
  alarm_flag=1;
}
 
void setup()
{
  pinMode(3, INPUT);           // set pin to input
  digitalWrite(3, HIGH);       // turn on pullup resistors
 
  Serial.begin(9600);
 
  /* setup int on pin 3 of arduino */
  attachInterrupt(1, blink, FALLING);
  /* clear out all the registers */
  rtc.initClock();
  /* set a time to start with.
   * day, weekday, month, century, year */
  rtc.setDate(14, 6, 3, 0, 10);
  /* hr, min, sec */
  rtc.setTime(1, 15, 40);
  /* set an alarm for 20 secs later...
   * alarm pin goes low when match occurs
   * this triggers the interrupt routine
   * min, hr, day, weekday 
   * 99 = no alarm value to be set
   */
  rtc.setAlarm(16, 99, 99, 99);
}
 
void loop()
{
  /* each sec update the display */
  Serial.print(rtc.formatTime());
  Serial.print("  ");
  Serial.print(rtc.formatDate());
  Serial.print("  0x");
  Serial.print(rtc.getStatus2(), HEX);
  Serial.print("\r\n");
  delay(1000);
  if (alarm_flag==1){
    clr_alarm();
  }
 
}
 
void clr_alarm()
{
  detachInterrupt(1);
  Serial.print("blink!\r\n");
 
  rtc.clearAlarm();
  delay(1000);
  alarm_flag=0;
  attachInterrupt(1, blink, FALLING);
}

 

Links
PCF8563 RTC Board PCF8563T CMOS Real-time Clock/Calendar Development Module

Share

Arduino and DS3231 RTC example

The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal.
The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.

ds3231

ds3231

Features

Highly Accurate RTC Completely Manages All Timekeeping Functions
Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
Accuracy ±2ppm from 0°C to +40°C
Accuracy ±3.5ppm from -40°C to +85°C
Digital Temp Sensor Output: ±3°C Accuracy
Two Time-of-Day Alarms
Programmable Square-Wave Output Signal
Simple Serial Interface Connects to Most Microcontrollers
Fast (400kHz) I2C Interface
Battery-Backup Input for Continuous Timekeeping
Low Power Operation Extends Battery-Backup Run Time
3.3V Operation

 

 

Here is a schematic for a typical module

DS3231 schematic

DS3231 schematic

 

Connection

 Arduino Pins Module Pins 
 GND  GND
5v  VCC
A4  SDA
A5  SCL

 

Code

This uses the RTCLib – https://github.com/adafruit/RTClib

#include <Wire.h>
#include "RTClib.h"
 
RTC_DS3231 rtc;
 
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void setup () {
 
#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
 
  Serial.begin(9600);
 
  delay(3000); // wait for console opening
 
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
 
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}
 
void loop () {
    DateTime now = rtc.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
 
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
 
    Serial.println();
    delay(3000);
}

Links
DS3231 AT24C32 IIC Module Precision Clock Module

Share

HT1621 6 Digit 7 segment Display example

This is a 6-digit 7-segment LCD display module and this screen is already widely used in multimeter, electronic scales, electronic clock and ultrasonic distance measurement.

It is suitable for Ar duino motherboard and the other 5V mcu, and it has a backlight which can help you to read the screen clearly in the dark.
Features:

– Compatibility: can be directly connected to Arduino board, 51, AVR board.
– Backlight color: White
– Display Size: 1.4 inch
– Driver IC: HT1621
– Interface: 3-wire SPI
– Working Voltage: 4.7-5.2VDC
– Working Current: 0.4mA without backlight, and 4mA with backlight
– Applications: Thermometers, multimeter, electronic scales, DIY projects, etc.

 

5 Digit 7 segment Display,

5 Digit 7 segment Display

Connection

I used the following connection for the module, you can see the module pins in the image above

Arduino Pin Module Pin
2 CS
3 WR
4 Data
Arduino Gnd Gnd
Arduino 5v Vcc

You can change the Arduino pins if you wish, just remember and change the defines below in the code

#define CS 2 //Pin 2 as chip selection output
#define WR 3 //Pin 3 as read clock output
#define DATA 4 //Pin 4 as Serial data output

Code

There was a lot of incomplete code examples, libraries with examples that did not work but i found this code example that requires no external libraries and worked nicely as a starting point

#define CS   2  //Pin 2 as chip selection output
#define WR   3  //Pin 3 as read clock output
#define DATA 4  //Pin 4 as Serial data output
 
#define CS1    digitalWrite(CS, HIGH) 
#define CS0    digitalWrite(CS, LOW)
#define WR1    digitalWrite(WR, HIGH) 
#define WR0    digitalWrite(WR, LOW)
#define DATA1  digitalWrite(DATA, HIGH) 
#define DATA0  digitalWrite(DATA, LOW)
 
#define sbi(x, y)  (x |= (1 << y))  
#define cbi(x, y)  (x &= ~(1 <<y ))      
#define uchar   unsigned char 
#define uint   unsigned int 
 
#define  ComMode    0x52  
#define  RCosc      0x30  
#define  LCD_on     0x06 
#define  LCD_off    0x04 
#define  Sys_en     0x02 
#define  CTRl_cmd   0x80
#define  Data_cmd   0xa0   
 
 
/*0,1,2,3,4,5,6,7,8,9,A,b,C,c,d,E,F,H,h,L,n,N,o,P,r,t,U,-, ,*/
const char num[]={0x7D,0x60,0x3E,0x7A,0x63,0x5B,0x5F,0x70,0x7F,0x7B,0x77,0x4F,0x1D,0x0E,0x6E,0x1F,0x17,0x67,0x47,0x0D,0x46,0x75,0x37,0x06,0x0F,0x6D,0x02,0x00,};
char dispnum[6]={0x00,0x00,0x00,0x00,0x00,0x00};
 
 
void SendBit_1621(uchar sdata,uchar cnt) 
{ 
  uchar i; 
  for(i=0;i<cnt;i++) 
  { 
    WR0;
    if(sdata&0x80) DATA1; 
    else DATA0;
    WR1;
    sdata<<=1; 
  } 
}
 
void SendCmd_1621(uchar command)
{ 
  CS0; 
  SendBit_1621(0x80,4);   
  SendBit_1621(command,8);
  CS1;                     
}
 
void Write_1621(uchar addr,uchar sdata)
{ 
  addr<<=2; 
  CS0; 
  SendBit_1621(0xa0,3);    
  SendBit_1621(addr,6);     
  SendBit_1621(sdata,8);    
  CS1; 
} 
 
void HT1621_all_off(uchar num)
{
  uchar i; 
  uchar addr=0; 
  for(i=0;i<num;i++) 
  { 
    Write_1621(addr,0x00); 
    addr+=2; 
  } 
}
 
void HT1621_all_on(uchar num)
{
  uchar i; 
  uchar addr=0; 
  for(i=0;i<num;i++) 
  {
    Write_1621(addr,0xff); 
    addr+=2; 
  } 
}
 
void Init_1621(void)
{
  SendCmd_1621(Sys_en);
  SendCmd_1621(RCosc);    
  SendCmd_1621(ComMode);  
  SendCmd_1621(LCD_on);
}    
 
void displaydata(int p)
{
  uchar i=0;
  switch(p)
  {
    case 1:
    sbi(dispnum[0],7);
    break;
    case 2:
    sbi(dispnum[1],7);
    break;
    case 3:
    sbi(dispnum[2],7);
    break;
    default:break;
  }
  for(i=0;i<=5;i++) 
  {
    Write_1621(i*2,dispnum[i]);
  }
}
 
 
 
void setup() {
  pinMode(CS, OUTPUT); // 
  pinMode(WR, OUTPUT); // 
  pinMode(DATA, OUTPUT); //
  CS1;
  DATA1;
  WR1;
  delay(50);
  Init_1621();
  HT1621_all_on(16); 
  delay(1000);
  HT1621_all_off(16);
  delay(1000);
 
  displaydata(1);//light on the first decimal point starting from the right side
  dispnum[5]=num[5];
  dispnum[4]=num[4];
  dispnum[3]=num[3];
  dispnum[2]=num[2];
  dispnum[1]=num[1];
  dispnum[0]=num[0];
 
  sbi(dispnum[5],7);  
  //cbi(dispnum[5],7);
  sbi(dispnum[4],7);   
  //cbi(dispnum[4],7); 
  sbi(dispnum[3],7);   
  //cbi(dispnum[3],7); 
 
 
 
  Write_1621(0,num[0]);  //0
  Write_1621(2,num[1]);  //1
  Write_1621(4,num[2]);  //2
  Write_1621(6,num[3]);  //3
  Write_1621(8,num[4]);  //4
  Write_1621(10,num[5]); //第5
 
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
}

 

Pretty straightforward, you should see 543210 on your display

 

Links

Fairly low cost for an LCD, coming in at about $5
LCD Module 2.4 inch 6-Digit 7 Segment LCD Display Module HT1621 LCD Driver IC with Decimal Point White Backlight for Arduino

Share