Categories

Follow Us

Reading temperature using the DS3231 RTC

Did you know that you can read the temperature from a DS3231 RTC – the DS3231 uses temperature compensation to calibrate the adjustable capacitors of its resonance circuit, in order to maintain time and date with accuracy

The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperaturecompensated crystal oscillator (TCXO) and crystal.
The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device as well as reduces the piece-part count in a manufacturing line.

The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially through an I2C bidirectional bus.

A precision temperature-compensated voltage reference and comparator circuit monitors the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the RST pin is monitored as a pushbutton input for generating a μP reset.

Some specific info from the datasheet

Temperature Registers (11h–12h)

Temperature is represented as a 10-bit code with a resolution of 0.25°C and is accessible at location 11h and 12h. The temperature is encoded in two’s complement format. The upper 8 bits, the integer portion, are at location 11h and the lower 2 bits, the fractional portion, are in the upper nibble at location 12h. For example, 0001100101b = +25.25°C. Upon power reset, the registers are set to a default temperature of 0°C and the controller starts a temperature conversion.

The temperature is read on initial application of VCC or I2C access on VBAT and once every 64 seconds afterwards. The temperature registers are updated after each user-initiated conversion and on every 64-second conversion. The temperature registers are read-only.

Parts List

name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
DS3231 RTC
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Connection

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

 

Code

This example requires the https://github.com/Makuna/Rtc

// These tests do not rely on RTC hardware at all
 
//#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
 
void PrintPassFail(bool passed)
{
    if (passed)
    {
      Serial.print("passed");
    }
    else
    {
      Serial.print("failed");
    }
}
 
void ComparePrintlnPassFail(RtcTemperature& rtcTemp, float compare)
{
    Serial.print(rtcTemp.AsFloatDegC());
    Serial.print("C ");
    PrintPassFail(rtcTemp.AsFloatDegC() == compare);
    Serial.println();
}
 
void ConstructorTests()
{
    // RTC constructors
    Serial.println("Constructors:");
    {
      RtcTemperature temp075Below(0b11111111, 0b01000000); // -0.75 
      ComparePrintlnPassFail(temp075Below, -0.75f);
 
      RtcTemperature temp050Below(0b11111111, 0b10000000); // -0.5 
      ComparePrintlnPassFail(temp050Below, -0.50f);
 
      RtcTemperature temp025Below(0b11111111, 0b11000000); // -0.25 
      ComparePrintlnPassFail(temp025Below, -0.25f);
 
      RtcTemperature tempZero(0b00000000, 0b00000000); // 0.0 
      ComparePrintlnPassFail(tempZero, -0.0f);
 
      RtcTemperature temp025Above(0b00000000, 0b01000000); // 0.25 
      ComparePrintlnPassFail(temp025Above, 0.25f);
 
      RtcTemperature temp050Above(0b00000000, 0b10000000); // 0.5 
      ComparePrintlnPassFail(temp050Above, 0.5f);
 
      RtcTemperature temp075Above(0b00000000, 0b11000000); // 0.75 
      ComparePrintlnPassFail(temp075Above, 0.75f);
 
      RtcTemperature temp25Above(0b00011001, 0b00000000); // 25.0
      ComparePrintlnPassFail(temp25Above, 25.0f);
 
      RtcTemperature temp25Below(0b11100111, 0b00000000); // -25.0
      ComparePrintlnPassFail(temp25Below, -25.0f);
    }
    Serial.println();
 
    // SameType
    {
      Serial.print("same type ");
      RtcTemperature temp25Below(0b11100111, 0b00000000); // -25.0
      RtcTemperature test = temp25Below;
      ComparePrintlnPassFail(test, -25.0f);
    }
 
    // CentiDegrees
    {
      Serial.print("centi degrees ");
      RtcTemperature temp025Below(-25); // -0.25
      ComparePrintlnPassFail(temp025Below, -0.25f);
 
      Serial.print("centi degrees ");
      RtcTemperature temp025Above(25); // 0.25
      ComparePrintlnPassFail(temp025Above, 0.25f);
 
      Serial.print("centi degrees ");
      RtcTemperature temp25Below(-2500); // -25.0
      ComparePrintlnPassFail(temp25Below, -25.0f);
 
      Serial.print("centi degrees ");
      RtcTemperature temp25Above(2500); // 25.0
      ComparePrintlnPassFail(temp25Above, 25.0f);
    }
 
    Serial.println();
}
 
void PrintlnExpected(RtcTemperature& temp, uint16_t digits)
{
  Serial.print(" = ");
  Serial.print(temp.AsFloatDegC(), digits);
  Serial.println();
}
 
void PrintTests()
{
  Serial.println("Prints:");
 
  RtcTemperature temp25Above(2500);
  temp25Above.Print(Serial);
  PrintlnExpected(temp25Above, 2);
 
  RtcTemperature temp25Below(-2500);
  temp25Below.Print(Serial);
  PrintlnExpected(temp25Below, 2);
 
  RtcTemperature temp025Above(25);
  temp025Above.Print(Serial);
  PrintlnExpected(temp025Above, 2);
  temp025Above.Print(Serial, 1);
  PrintlnExpected(temp025Above, 1);
 
  RtcTemperature temp025Below(-25);
  temp025Below.Print(Serial);
  PrintlnExpected(temp025Below, 2);
  temp025Below.Print(Serial, 1);
  PrintlnExpected(temp025Below, 1);
 
  RtcTemperature temp050Above(50);
  temp050Above.Print(Serial);
  PrintlnExpected(temp050Above, 2);
  temp050Above.Print(Serial, 0);
  PrintlnExpected(temp050Above, 0);
 
  RtcTemperature temp050Below(-50);
  temp050Below.Print(Serial);
  PrintlnExpected(temp050Below, 2);
  temp050Below.Print(Serial, 0);
  PrintlnExpected(temp050Below, 0);
  temp050Below.Print(Serial, 2, ',');
  Serial.println(" == -0,50");
 
  Serial.println();
}
 
void MathmaticalOperatorTests()
{
  Serial.println("Mathmaticals:");
 
  RtcTemperature temp050Below(-50);
  RtcTemperature temp050Above(50);
  RtcTemperature temp050Diff(100);
  RtcTemperature temp050Same(-50);
  RtcTemperature tempResult;
 
  Serial.print("equality ");
  PrintPassFail(temp050Below == temp050Same);
  Serial.println();
 
  Serial.print("inequality ");
  PrintPassFail(temp050Below != temp050Above);
  Serial.println();
 
  Serial.print("less than ");
  PrintPassFail(temp050Below < temp050Above);
  Serial.println();
 
  Serial.print("greater than ");
  PrintPassFail(temp050Above > temp050Below);
  Serial.println();
 
  Serial.print("less than ");
  PrintPassFail(temp050Below <= temp050Above);
  Serial.print(" or equal ");
  PrintPassFail(temp050Below <= temp050Same);
  Serial.println();
 
  Serial.print("greater than ");
  PrintPassFail(temp050Above >= temp050Below);
  Serial.print(" or equal ");
  PrintPassFail(temp050Below >= temp050Same);
  Serial.println();
 
  tempResult = temp050Above - temp050Below;
  Serial.print("subtraction ");
  PrintPassFail(tempResult == temp050Diff);
  Serial.println();
 
  tempResult = temp050Above + temp050Above;
  Serial.print("addition ");
  PrintPassFail(tempResult == temp050Diff);
  Serial.println();
 
  Serial.println();
}
 
void setup () 
{
    Serial.begin(115200);
    while (!Serial);
    Serial.println();
 
    ConstructorTests();
    PrintTests();
    MathmaticalOperatorTests();
}
 
void loop () 
{
    delay(500);
}

 

Output

Open the serial monitor window and you will see something like this

Constructors:
-0.75C passed
-0.50C passed
-0.25C passed
0.00C passed
0.25C passed
0.50C passed
0.75C passed
25.00C passed
-25.00C passed

same type -25.00C passed
centi degrees -0.25C passed
centi degrees 0.25C passed
centi degrees -25.00C passed
centi degrees 25.00C passed

Prints:
25.00 = 25.00
-25.00 = -25.00
0.25 = 0.25
0.3 = 0.3
-0.25 = -0.25
-0.3 = -0.3
0.50 = 0.50
1 = 1
-0.50 = -0.50
-1 = -1
-0,50 == -0,50

Mathmaticals:
equality passed
inequality passed
less than passed
greater than passed
less than passed or equal passed
greater than passed or equal passed
subtraction passed
addition passed

Link

https://www.silabs.com/documents/public/data-sheets/Si1145-46-47.pdf

Share

34 Arduino Sensor Projects ebook

We have just written an ebook and published it on the Amazon Kindle store, the topic of this ebook was looking at a variety of sensors with the Arduino IDE

We have written this on the basis that you have some basic knowledge of installing and using the Arduino IDE and know a little about the hardware but want to look at various sensors, each sensor or chapter has a description of the sensor, schematics or connection, source code and where applicable the output. the code examples are also on github, the link is available in the ebook.

Overview 4
Arduino Basics 5
Sensor 1 – BMP180 7
Sensor 2 – DS18B20 11
Sensor 3 – DHT11 14
Sensor 4 – PIR Detector 18
Sensor 5 – HC-SR04 Ultrasonic Sensor 21
Sensor 6 – HDC1008 24
Sensor 7 – mcp9808 26
Sensor 8 – mlx90614 30
Sensor 9 – BME280 32
Sensor 10 – TSL2561 35
Sensor 11 –SHT21 38
Sensor 12 – ML8511 41
Sensor 13 – SHT31 43
Sensor 14 – MAG3110 46
Sensor 15 – TCS34725 50
Sensor 16 – MLX90393 54
Sensor 17 – DS1624 58
Sensor 18 – SI7021 61
Sensor 19 – HDC1080 65
Sensor 20 – BMP280 68
Sensor 21 – DHT12 72
Sensor 22 – AM2320 76
Sensor 23 – LM75 79
Sensor 24 – OPT3001 82
Sensor 25 – TMP175 87
Sensor 26 – MPL3115A2 91
Sensor 27 – CCS811 95
Sensor 28 – MAX30100 99
Sensor 29 – MLX90615 102
Sensor 30 – Si1145 105
Sensor 31 – TMP102 109
Sensor 32 – HMC5983 113
Sensor 33 – MAX44009 116
Sensor 34 – MMA8451 119

We recieve a percentage of the profits from Amazon which we reinvest into this site with regards domain name renewal, web site hosting and purchasing any parts we use – we don’t get any samples or freebies from any companies.

So please support the site by purchasing the ebook from the following links

Site Link
Amazon US Buy here
Amazon UK Buy here
Amazon DE Buy here
Amazon FR Buy here
Amazon ES Buy here
Amazon IT Buy here
Amazon NL Buy here
Amazon JP Buy here
Amazon BR Buy here
Amazon CA Buy here
Amazon MX Buy here
Amazon AU Buy here
Amazon IN Buy here
Share

2.8 Inch TFT LCD Shield Touch Display Screen Module For Arduino

I recently started looking through various shields I had no articles, code or tutorials for and came across a 2.8 Inch TFT LCD Shield Touch Display Screen Module For Arduino

Its a cracking display, here are some pictures of it


Features:

2.8 inch LCD TFT display
Bright, 4 white-LED backlight
Colorful, 18-bit 262,000 different shades
4-wire resistive touchscreen 240 x 320 pixels with individual pixel control
No wiring, no soldering.
Simply plug it in and load library
On-board 3.3V 300mA LDO regulator
5V compatible, use with 3.3V or 5V logic
Support 2GB micro SD TF card
Size: 7.8 x 5.3cm

 

Parts List

Name Link
Arduino Mega MEGA 2560 R3 (ATmega2560-16AU CH340G) AVR USB board Development board
2.8 LCD shield WAVGAT 2.8 Inch 3.3V 300mA TFT LCD Shield Touch Display Module For Arduino UNO With Resistive Touch Panel DIY Kit

 

Code

I connected the shield to an Arduino Mega

4 libraries are required with the code example that I tested with – I uploaded these to my github site

  1. // IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
  2. // CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
  3. // SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
  4. //Technical support:goodtft@163.com
  5.  
  6. #include <Adafruit_GFX.h> // Core graphics library
  7. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  8.  
  9. // The control pins for the LCD can be assigned to any digital or
  10. // analog pins...but we'll use the analog pins as this allows us to
  11. // double up the pins with the touch screen (see the TFT paint example).
  12. #define LCD_CS A3 // Chip Select goes to Analog 3
  13. #define LCD_CD A2 // Command/Data goes to Analog 2
  14. #define LCD_WR A1 // LCD Write goes to Analog 1
  15. #define LCD_RD A0 // LCD Read goes to Analog 0
  16.  
  17. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  18.  
  19. // When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
  20. // For the Arduino Uno, Duemilanove, Diecimila, etc.:
  21. // D0 connects to digital pin 8 (Notice these are
  22. // D1 connects to digital pin 9 NOT in order!)
  23. // D2 connects to digital pin 2
  24. // D3 connects to digital pin 3
  25. // D4 connects to digital pin 4
  26. // D5 connects to digital pin 5
  27. // D6 connects to digital pin 6
  28. // D7 connects to digital pin 7
  29. // For the Arduino Mega, use digital pins 22 through 29
  30. // (on the 2-row header at the end of the board).
  31.  
  32. // Assign human-readable names to some common 16-bit color values:
  33. #define BLACK 0x0000
  34. #define BLUE 0x001F
  35. #define RED 0xF800
  36. #define GREEN 0x07E0
  37. #define CYAN 0x07FF
  38. #define MAGENTA 0xF81F
  39. #define YELLOW 0xFFE0
  40. #define WHITE 0xFFFF
  41.  
  42. #include <MCUFRIEND_kbv.h>
  43. MCUFRIEND_kbv tft;
  44. // If using the shield, all control and data lines are fixed, and
  45. // a simpler declaration can optionally be used:
  46. // Adafruit_TFTLCD tft;
  47.  
  48. void setup(void) {
  49. Serial.begin(9600);
  50. Serial.println(F("TFT LCD test"));
  51.  
  52. #ifdef USE_ADAFRUIT_SHIELD_PINOUT
  53. Serial.println(F("Using Adafruit 2.4\" TFT Arduino Shield Pinout"));
  54. #else
  55. Serial.println(F("Using Adafruit 2.4\" TFT Breakout Board Pinout"));
  56. #endif
  57.  
  58. Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
  59.  
  60. tft.reset();
  61.  
  62. uint16_t identifier = tft.readID();
  63. if(identifier == 0x9325) {
  64. Serial.println(F("Found ILI9325 LCD driver"));
  65. } else if(identifier == 0x9328) {
  66. Serial.println(F("Found ILI9328 LCD driver"));
  67. } else if(identifier == 0x4535) {
  68. Serial.println(F("Found LGDP4535 LCD driver"));
  69. }else if(identifier == 0x7575) {
  70. Serial.println(F("Found HX8347G LCD driver"));
  71. } else if(identifier == 0x9341) {
  72. Serial.println(F("Found ILI9341 LCD driver"));
  73. }else if(identifier == 0x7783) {
  74. Serial.println(F("Found ST7781 LCD driver"));
  75. }else if(identifier == 0x8230) {
  76. Serial.println(F("Found UC8230 LCD driver"));
  77. }
  78. else if(identifier == 0x8357) {
  79. Serial.println(F("Found HX8357D LCD driver"));
  80. } else if(identifier==0x0101)
  81. {
  82. identifier=0x9341;
  83. Serial.println(F("Found 0x9341 LCD driver"));
  84. }else {
  85. Serial.print(F("Unknown LCD driver chip: "));
  86. Serial.println(identifier, HEX);
  87. Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
  88. Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
  89. Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
  90. Serial.println(F("If using the breakout board, it should NOT be #defined!"));
  91. Serial.println(F("Also if using the breakout, double-check that all wiring"));
  92. Serial.println(F("matches the tutorial."));
  93. identifier=0x9341;
  94.  
  95. }
  96.  
  97. tft.begin(identifier);
  98.  
  99.  
  100. }
  101.  
  102. void loop(void) {
  103. tft.fillScreen(BLACK);
  104. unsigned long start = micros();
  105. tft.setCursor(0, 0);
  106.  
  107. tft.setTextColor(RED); tft.setTextSize(1);
  108. tft.println("Hello World!");
  109. tft.println(01234.56789);
  110. tft.println(0xDEADBEEF, HEX);
  111. tft.println();
  112. tft.println();
  113. tft.setTextColor(GREEN); tft.setTextSize(2);
  114. tft.println("Hello World!");
  115. tft.println(01234.56789);
  116. tft.println(0xDEADBEEF, HEX);
  117. tft.println();
  118. tft.println();
  119.  
  120. tft.setTextColor(BLUE); tft.setTextSize(3);
  121. tft.println("Hello World!");
  122. tft.println(01234.56789);
  123. tft.println(0xDEADBEEF, HEX);
  124.  
  125. tft.setTextColor(WHITE); tft.setTextSize(4);
  126. tft.println("Hello!");
  127. tft.setTextColor(YELLOW); tft.setTextSize(5);
  128. tft.println("Hello!");
  129. tft.setTextColor(RED); tft.setTextSize(6);
  130. tft.println("Hello!");
  131. tft.println();
  132. tft.println();
  133. /*
  134.   tft.println();
  135.   tft.setTextColor(GREEN);
  136.   tft.setTextSize(5);
  137.   tft.println("Groop");
  138.   tft.setTextSize(2);
  139.   tft.println("I implore thee,");
  140.   tft.setTextSize(1);
  141.   tft.println("my foonting turlingdromes.");
  142.   tft.println("And hooptiously drangle me");
  143.   tft.println("with crinkly bindlewurdles,");
  144.   tft.println("Or I will rend thee");
  145.   tft.println("in the gobberwarts");
  146.   tft.println("with my blurglecruncheon,");
  147.   tft.println("see if I don't!");*/
  148. delay(1000);delay(1000);delay(1000);delay(1000);delay(1000);
  149. }
Share

Arduino Uno and PAJ7620 gesture sensor example

In this example we connect a PAJ7620 gesture sensor to an Arduino Uno

The PAJ7620 integrates gesture recognition function with general I2C interface into a single chip forming an image analytic sensor system. It can recognize 9 human hand gesticulations such as moving up, down, left, right, forward, backward, circle-clockwise, circle-counter clockwise, and waving.

It also offers built-in proximity detection in sensing approaching or departing object from the sensor. The PAJ7620 is designed with great flexibility in power-saving mechanism, well suit for low power battery operated HMI devices.

The PAJ7620 is packaged into module form in-built with IR LED and optics lens as a complete sensor solution.

Gesture Mode : Single-Object Detection
Image Array Size : 60 x 60
Sampling Rate : 240fps
Operating Distance : 20cm
Output Types : 9 types of Gestures: Up, Down, Left, Right, Forward, Backward, Circle-Clockwise, Circle-Counter Clockwise, and Waving)
Cursor Mode : Yes
Interface : I2C/SPI
Light Source : Integrated IR LED with optics

 

Parts List

name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
PAJ7620 breakout CJMCU-7620 9 Gesture Moving Recognition Sensor Module PAJ7620U2 I2C 2.8V~3.3V SC
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Layout

An I2C device that uses 3.3v

 

Code

Again we use a library – https://www.elecrow.com/wiki/images/7/72/Paj7620.zip

#include <Wire.h>
#include "paj7620.h"
 
void setup()
{
Serial.begin(9600);
paj7620Init();
}
 
void loop()
{
uint8_t data = 0; // Read Bank_0_Reg_0x43/0x44 for gesture result.
 
paj7620ReadReg(0x43, 1, &data);
 
if (data == GES_UP_FLAG)
{
Serial.println("GES_UP_FLAG !");
}
 
if (data == GES_DOWN_FLAG)
{
Serial.println("GES_DOWN_FLAG !");
}
 
if(data == GES_FORWARD_FLAG)
{
Serial.println("GES_FORWARD_FLAG !");
}
 
if(data == GES_BACKWARD_FLAG)
{
Serial.println("GES_BACKWARD_FLAG !");
}
 
if(data == GES_RIGHT_FLAG)
{
Serial.println("GES_RIGHT_FLAG !");
}
 
if(data ==GES_LEFT_FLAG)
{
Serial.println("GES_LEFT_FLAG !");
}
 
}

 

 

Output

Open the serial monitor – this is what I saw, move your hands in various directions

INIT SENSOR…
Addr0 =20, Addr1 =76
wake-up finish.
Paj7620 initialize register finished.
GES_BACKWARD_FLAG !
GES_BACKWARD_FLAG !
GES_FORWARD_FLAG !
GES_BACKWARD_FLAG !
GES_FORWARD_FLAG !
GES_BACKWARD_FLAG !
GES_UP_FLAG !
GES_DOWN_FLAG !
GES_UP_FLAG !
GES_DOWN_FLAG !

 

Links

http://www.pixart.com.tw/upload/PAJ7620U2_GDS_v1.0_29032016_20160623194552.pdf

Share