Last active
August 29, 2015 14:27
-
-
Save kjiwa/0b964182a9972ab5f9c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An Arduino program that reads a value from a temperature sensor, transmits it | |
* over an RF radio, and then enters a low-power sleep until repeating the | |
* process. | |
*/ | |
#include <SPI.h> | |
#include <nRF24L01.h> | |
#include <RF24.h> | |
#include <LowPower.h> | |
/** | |
* A number uniquely identifying the device. | |
*/ | |
#ifndef TRANSMITTER_DEVICE_ID | |
#define TRANSMITTER_DEVICE_ID 0 | |
#endif | |
/** | |
* The pin on which the temperature sensor receives its supply voltage. Pins 0 | |
* and 1 are used for serial RX and TX, respectively, so we use pin 2. | |
*/ | |
#ifndef TRANSMITTER_SENSOR_PIN | |
#define TRANSMITTER_SENSOR_PIN 2 | |
#endif | |
/** | |
* The pipe address to use when transmitting messages. Addresses are 40-bit | |
* values (0x0000000000000000 - 0x000000ffffffffff). | |
*/ | |
#ifndef TRANSMITTER_PIPE_ADDR | |
#define TRANSMITTER_PIPE_ADDR 0x0000000000000000 | |
#endif | |
/** | |
* The temperature in Kelvin at which the thermistor on the temperature sensor | |
* has a resistance of 10000 ohms. | |
*/ | |
#ifndef TRANSMITTER_THERMISTOR_T0 | |
#define TRANSMITTER_THERMISTOR_T0 298.15 | |
#endif | |
/** | |
* The B-valuein Kelvin of the thermistor on the temperature sensor. | |
*/ | |
#ifndef TRANSMITTER_THERMISTOR_B | |
#define TRANSMITTER_THERMISTOR_B 3950 | |
#endif | |
/** | |
* A message containing sensor readings. The structure is: | |
* | |
* 0: A number uniquely identifying the device. | |
* 1: The temperature in Kelvin. | |
*/ | |
static int msg[2]; | |
/** | |
* A handle to the RF transceiver. The transceiver requires a 3.3V supply | |
* voltage and is connected in the following way: | |
* | |
* CE: 9 | |
* CSN: 10 | |
* MOSI: 11 | |
* MISO: 12 | |
* SCK: 13 | |
*/ | |
static RF24 radio(9, 10); | |
static double temperature(); | |
void setup() { | |
msg[0] = TRANSMITTER_DEVICE_ID; | |
/* Initialize the RF radio. */ | |
radio.begin(); | |
radio.openWritingPipe(TRANSMITTER_PIPE_ADDR); | |
/* Supply 5V to the temperature sensor. */ | |
pinMode(TRANSMITTER_SENSOR_PIN, OUTPUT); | |
digitalWrite(TRANSMITTER_SENSOR_PIN, HIGH); | |
} | |
void loop() { | |
msg[1] = temperature(); | |
radio.write(msg, sizeof(msg)); | |
radio.powerDown(); | |
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); | |
} | |
/** | |
* Reads the temperature from the circuit connected to pin A0. | |
* | |
* The circuit connected to pin A0 is a voltage divider with a 5V input voltage, | |
* a 10k ohm resistor, and a 10k ohm thermistor with T0 = 298.15K and B = 3950K. | |
* We calculate the temperature in Kelvin by using the voltage divider and | |
* Steinhart-Hart equations: | |
* | |
* R = (10000 * V) / (5 - V) | |
* T = 1 / ((1 / 298.15) + (ln(R / 10000) / 3950)) | |
* = 1 / ((1 / 298.15) + (ln(V / (5 - V)) / 3950)) | |
* | |
* Note: When plugged in to USB, the digital pins supply a measured voltage of | |
* 4.92V. | |
* | |
* @return The measured temperature in Kelvin. | |
*/ | |
static double temperature() { | |
/* | |
* The Arduino returns a value in the range [0, 1023] to represent voltages in | |
* the range [0, 5]. | |
*/ | |
double voltage = ((double)analogRead(A0) / 1023) * 5; | |
return 1 / ((1 / TRANSMITTER_THERMISTOR_T0) + | |
(log(voltage / (5 - voltage)) / TRANSMITTER_THERMISTOR_B)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment