Skip to content

Commit

Permalink
simplify begin (#15)
Browse files Browse the repository at this point in the history
- simplify begin()
- updated all examples
- update readme.md
  • Loading branch information
RobTillaart authored Oct 25, 2023
1 parent 3660205 commit b1ed8ae
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 79 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2023-10-25
- simplify begin()
- updated all examples
- update readme.md

----

## [0.2.3] - 2023-07-27
- fix #12 add time out to read function


## [0.2.2] - 2022-12-21
- update keywords.txt
- add defaults to offset functions.
Expand Down
18 changes: 1 addition & 17 deletions DHT20.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: DHT20.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.3
// VERSION: 0.3.0
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.


Expand Down Expand Up @@ -31,27 +31,11 @@ DHT20::DHT20(TwoWire *wire)

bool DHT20::begin()
{
_wire->begin();
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}


#if defined(ESP8266) || defined(ESP32)
bool DHT20::begin(const uint8_t dataPin, const uint8_t clockPin)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
// _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true);
return isConnected();
}
#endif


bool DHT20::isConnected()
{
_wire->beginTransmission(DHT20_ADDRESS);
Expand Down
8 changes: 2 additions & 6 deletions DHT20.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
// VERSION: 0.2.3
// VERSION: 0.3.0
// URL: https://github.com/RobTillaart/DHT20
//

Expand All @@ -20,7 +20,7 @@
#include "Arduino.h"
#include "Wire.h"

#define DHT20_LIB_VERSION (F("0.2.3"))
#define DHT20_LIB_VERSION (F("0.3.0"))

#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
Expand All @@ -38,10 +38,6 @@ class DHT20
// fixed address 0x38
DHT20(TwoWire *wire = &Wire);

// start the I2C
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin();
bool isConnected();
uint8_t getAddress();
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
[![Arduino CI](https://github.com/RobTillaart/DHT20/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/DHT20.svg)](https://github.com/RobTillaart/DHT20/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHT20/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DHT20.svg?maxAge=3600)](https://github.com/RobTillaart/DHT20/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/DHT20.svg)](https://registry.platformio.org/libraries/robtillaart/DHT20)


# DHT20
Expand Down Expand Up @@ -52,6 +55,11 @@ reset the sensor if needed in both synchronous and asynchronous calls.
This keeps the API simple. The reads are 1-2 ms slower than 0.1.4. (< 50 ms).
Still far below the 80 ms mentioned in the datasheet.

### 0.3.0

User should call Wire.begin(), and setting I2C pins himself.
It is removed from the specific ESP32 begin() call to be more generic.


#### Tested

Expand Down Expand Up @@ -140,7 +148,6 @@ and should only be used if you are in a need for speed.
#### Constructor

- **DHT20(TwoWire \*wire = &Wire)** constructor, using a specific Wire (I2C bus).
- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins.
- **bool begin()** initializer for non ESP32. Returns true if connected.
- **bool isConnected()** returns true if the address of the DHT20 can be seen on the I2C bus.
- **uint8_t getAddress()** returns the (fixed) address - convenience.
Expand Down Expand Up @@ -269,3 +276,12 @@ the read calls. (0.2.0)
- **bool getIgnoreChecksum()** get checksum flag. for completeness.


## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,


13 changes: 7 additions & 6 deletions examples/DHT20/DHT20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

// Always check datasheet - front view
//
// +--------------+
Expand All @@ -22,14 +21,16 @@ uint8_t count = 0;

void setup()
{
DHT.begin(); // ESP32 default pins 21 22

Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();

Wire.begin();
DHT.begin(); // ESP32 default pins 21 22


delay(1000);
}

Expand All @@ -38,7 +39,7 @@ void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// READ DATA
// READ DATA
uint32_t start = micros();
int status = DHT.read();
uint32_t stop = micros();
Expand All @@ -52,7 +53,7 @@ void loop()
count++;

Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Expand Down Expand Up @@ -91,4 +92,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
4 changes: 2 additions & 2 deletions examples/DHT20_I2C_speed/DHT20_I2C_speed.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

// Always check datasheet - front view
//
// +--------------+
Expand Down Expand Up @@ -31,6 +30,7 @@ void setup()

Serial.println("\nNOTE: datasheet states 400 KHz as maximum.\n");

Wire.begin();
DHT.begin(); // ESP32 default pins 21, 22
delay(2000);

Expand Down Expand Up @@ -98,4 +98,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
13 changes: 6 additions & 7 deletions examples/DHT20_async/DHT20_async.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

// Always check datasheet - front view
//
// +--------------+
Expand All @@ -22,21 +21,21 @@ uint32_t counter = 0;

void setup()
{
DHT.begin(); // ESP32 default 21, 22

Wire.setClock(400000);

Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();

Wire.begin();
Wire.setClock(400000);

DHT.begin(); // ESP32 default 21, 22

delay(2000);

// start with initial request
Serial.println(DHT.requestData());

}


Expand Down Expand Up @@ -65,4 +64,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
20 changes: 10 additions & 10 deletions examples/DHT20_lcd/DHT20_lcd.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

// Always check datasheet - front view
//
// +--------------+
Expand Down Expand Up @@ -57,16 +56,16 @@ uint32_t stop, start;

int displayAddress = 0x27;

// lcd object is created as a placeholder as the actual address is determined in setupDisplay()
// constructor needs an I2C address. Better solution would be a function to set the address
// runtime in the class.
// LCD object is created as a placeholder as the actual address is determined in setupDisplay()
// constructor needs an I2C address. Better solution would be a function to set the address
// runtime in the class.
LiquidCrystal_I2C lcd(displayAddress);

void setupDisplay()
{
lcd = LiquidCrystal_I2C(displayAddress, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
lcd.begin(20, 4);
// lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE);
// lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(BL_ON);
}
Expand Down Expand Up @@ -97,14 +96,15 @@ void display()

void setup()
{
DHT.begin(); // ESP32 default pins 21 22

Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();

Wire.begin();
DHT.begin(); // ESP32 default pins 21 22

setupDisplay();

delay(1000);
Expand All @@ -115,7 +115,7 @@ void loop()
{
if (millis() - DHT.lastRead() >= 1000)
{
// READ DATA
// READ DATA
start = micros();
int status = DHT.read();
stop = micros();
Expand All @@ -131,7 +131,7 @@ void loop()
count++;

Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Expand Down Expand Up @@ -170,4 +170,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
16 changes: 8 additions & 8 deletions examples/DHT20_offset/DHT20_offset.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
//

// Always check datasheet - front view
//
// +--------------+
Expand All @@ -22,16 +21,17 @@ uint8_t count = 0;

void setup()
{
DHT.begin(); // ESP32 default pins 21 22

Wire.setClock(400000);

Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DHT20 LIBRARY VERSION: ");
Serial.println(DHT20_LIB_VERSION);
Serial.println();

Wire.begin();
Wire.setClock(400000);

DHT.begin(); // ESP32 default pins 21 22

delay(1000);
}

Expand All @@ -48,7 +48,7 @@ void loop()
// make a measurement every 2 seconds
if (millis() - DHT.lastRead() >= 2000)
{
// READ DATA
// READ DATA
uint32_t start = micros();
int status = DHT.read();
uint32_t stop = micros();
Expand All @@ -62,7 +62,7 @@ void loop()
count++;

Serial.print("DHT20 \t");
// DISPLAY DATA, sensor has only one decimal.
// DISPLAY DATA, sensor has only one decimal.
Serial.print(DHT.getHumidity(), 1);
Serial.print("\t\t");
Serial.print(DHT.getTemperature(), 1);
Expand Down Expand Up @@ -105,4 +105,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
Loading

0 comments on commit b1ed8ae

Please sign in to comment.