Skip to content

Commit

Permalink
Fix NTCSensor enable pin bug (#1)
Browse files Browse the repository at this point in the history
* Fix NTC enable pin bug

Do not change any pinmodes or values if enablePin is not in use when reading temperature.

* Add version history

* Update README.md
  • Loading branch information
aattww committed Dec 29, 2019
1 parent 1292d99 commit 13cd353
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Skip directly to [Instructions](#instructions), although I strongly recommend re
* [Battery](#battery-3)
* [Pulse / Pulse with Kamstrup Multical](#pulse--pulse-with-kamstrup-multical-1)
* [Instructions](#instructions)
* [Version history](#version-history)

# External requirements

Expand Down Expand Up @@ -417,3 +418,13 @@ Place your gateway to a central location and connect it to a Modbus capable netw
Distribute other nodes as needed, selecting first their addresses with jumper headers and then connecting external power or batteries. Use the button on the nodes to force a transmit with full power - normally nodes adjust their transmit power automatically to the lowest possible level. One blink of the onboard LED indicates a successful transmit, two blinks a failed one. LED blinks only when forcing a transmit with the button. You can use the provided Python script [*read_modbus.py*](read_modbus.py) to read data from the gateway for debugging purposes.

Start logging measurements to a MySQL database ([*save_modbus_to_db.py*](save_modbus_to_db.py) provides a starting point for this), for example, and graph it with [Grafana](https://grafana.com/), or connect the gateway to a home automation hub and monitor measurements that way.

# Version history

## v1.0.1 (2019-12-29)

* Fixed a bug in NTCSensor library where enable pin was controlled even though it was not in actual use, possibly interfering with serial communication.

## v1.0.0 (2019-12-26)

Initial public release.
4 changes: 2 additions & 2 deletions SensorsGateway/SensorsGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@


#define MAJOR_VERSION 1
#define MINOR_VERSION 1
#define MINOR_VERSION 2

#include <SimpleModbusAsync.h>
#include <RH_RF95.h>
Expand Down Expand Up @@ -120,7 +120,7 @@ RHReliableDatagram radioManager(rf95Driver);
SimpleModbusAsync modbus;

// NTC instance
NTCSensor sensorNTC(0, P3_PIN);
NTCSensor sensorNTC(NTC_NO_ENABLE_PIN, P3_PIN);

// Memory handler instance
SensorsMemoryHandler memoryHandler(SRAM_NSS);
Expand Down
4 changes: 2 additions & 2 deletions SensorsPulse/SensorsPulse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
*/


#define VERSION 1
#define VERSION 2

#if defined NODE_TYPE_MULTICAL
#include <SimpleModbusAsync.h>
Expand Down Expand Up @@ -144,7 +144,7 @@ RHReliableDatagram radioManager(rf95Driver);
SimpleModbusAsync modbus;
#endif

NTCSensor sensorNTC(0, P3_PIN);
NTCSensor sensorNTC(NTC_NO_ENABLE_PIN, P3_PIN);

// Payload buffer
uint8_t payloadBuffer[PAYLOAD_LEN];
Expand Down
39 changes: 27 additions & 12 deletions libraries/NTCSensor/NTCSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,38 @@
*
* Parameters:
* enablePin - Enable voltage divider and power on thermistor
* 255 = Not in use, thermistor is always pulled up by external resistor to Vcc
* !=255 = Pin connected to high side of divider
* NTC_NO_ENABLE_PIN = Not in use, thermistor is always pulled up by external resistor to Vcc
* ARDUINO PIN = Pin connected to high side of divider
* sensorPin - Thermistor
*
* Schematic (enablePin != 255):
* Schematic (enablePin != NTC_NO_ENABLE_PIN):
* [GND]---[NTC]---|---[SERIES_RESISTOR]---[enablePin]
* |
* [sensorPin]
*
* Schematic (enablePin == 255):
* Schematic (enablePin == NTC_NO_ENABLE_PIN):
* [GND]---[NTC]---|---[SERIES_RESISTOR]---[Vcc]
* |
* [sensorPin]
*/

/*
* Version history
* ---------------
*
* 1.1 2019-12-29 (CURRENT)
* - Fixed a bug when enablePin was not in use but placeholder pin was still controlled.
*
* 1.0 2019-12-26
* Initial public release
*/

#include "NTCSensor.h"

/*
* Creates a new instance.
*
* enablePin: voltage divider enable pin (set to 255 if not in use)
* enablePin: voltage divider enable pin (set to NTC_NO_ENABLE_PIN if not in use)
* sensorPin: thermistor pin
*
* returns: no
Expand Down Expand Up @@ -83,7 +94,7 @@ bool NTCSensor::init() {
// Go through different scenarios to determine if thermistor is connected properly

// If thermistor does not have a separate enable pin (thermistor should always be powered)
if (_enablePin == 255) {
if (_enablePin == NTC_NO_ENABLE_PIN) {
// Enable internal pullup resistor so that pin is not floating if there is no thermistor
pinMode(_sensorPin, INPUT_PULLUP);
delay(50);
Expand Down Expand Up @@ -136,7 +147,7 @@ bool NTCSensor::init() {
}

/*
* Reads current temperature.
* Reads current temperature in Celsius.
*
* parameters: no
*
Expand All @@ -153,9 +164,11 @@ int16_t NTCSensor::readTemperature() {
analogReference(DEFAULT);
analogRead(_sensorPin);

// Turn on analog voltage divider
digitalWrite(_enablePin, HIGH);
delay(50);
// Turn on analog voltage divider if it is in use
if (_enablePin != NTC_NO_ENABLE_PIN) {
digitalWrite(_enablePin, HIGH);
delay(50);
}

float average = 0.0;

Expand All @@ -165,8 +178,10 @@ int16_t NTCSensor::readTemperature() {
delay(10);
}

// Turn off analog voltage
digitalWrite(_enablePin, LOW);
// Turn off analog voltage if in use
if (_enablePin != NTC_NO_ENABLE_PIN) {
digitalWrite(_enablePin, LOW);
}

// Calculate average
average /= 5.0;
Expand Down
27 changes: 20 additions & 7 deletions libraries/NTCSensor/NTCSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,39 @@
*
* Parameters:
* enablePin - Enable voltage divider and power on thermistor
* 255 = Not in use, thermistor is always pulled up by external resistor to Vcc
* !=255 = Pin connected to high side of divider
* NTC_NO_ENABLE_PIN = Not in use, thermistor is always pulled up by external resistor to Vcc
* ARDUINO PIN = Pin connected to high side of divider
* sensorPin - Thermistor
*
* Schematic (enablePin != 255):
* Schematic (enablePin != NTC_NO_ENABLE_PIN):
* [GND]---[NTC]---|---[SERIES_RESISTOR]---[enablePin]
* |
* [sensorPin]
*
* Schematic (enablePin == 255):
* Schematic (enablePin == NTC_NO_ENABLE_PIN):
* [GND]---[NTC]---|---[SERIES_RESISTOR]---[Vcc]
* |
* [sensorPin]
*/

/*
* Version history
* ---------------
*
* 1.1 2019-12-29 (CURRENT)
* - Fixed a bug when enablePin was not in use but placeholder pin was still controlled.
*
* 1.0 2019-12-26
* Initial public release
*/

#ifndef NTCSENSOR_H
#define NTCSENSOR_H

#include "Arduino.h"

#define NTC_NO_ENABLE_PIN 255

// Resistance at 25 C
#define NOMINAL_RESISTANCE 10000.0

Expand All @@ -69,7 +82,7 @@ class NTCSensor {

private:

uint8_t _enablePin; // Voltage divider enable pin (255 if not in use)
uint8_t _enablePin; // Voltage divider enable pin (NTC_NO_ENABLE_PIN if not in use)
uint8_t _sensorPin; // Thermistor pin
bool _initialised; // Sensor has been initialized

Expand All @@ -78,7 +91,7 @@ class NTCSensor {
/*
* Creates a new instance.
*
* enablePin: voltage divider enable pin (set to 255 if not in use)
* enablePin: voltage divider enable pin (set to NTC_NO_ENABLE_PIN if not in use)
* sensorPin: thermistor pin
*
* returns: no
Expand All @@ -99,7 +112,7 @@ class NTCSensor {
bool init();

/*
* Reads current temperature.
* Reads current temperature in Celsius.
*
* parameters: no
*
Expand Down

0 comments on commit 13cd353

Please sign in to comment.