Skip to content

Instantly share code, notes, and snippets.

@nigelhanlon
Created April 30, 2017 14:03
Show Gist options
  • Save nigelhanlon/6f6231330df463ce31ccd519fc3f13d0 to your computer and use it in GitHub Desktop.
Save nigelhanlon/6f6231330df463ce31ccd519fc3f13d0 to your computer and use it in GitHub Desktop.
A simple node.js script for reading temperatures via Arduino Serial port.
var SerialPort = require('serialport');
var port = new SerialPort("/dev/ttyACM0", {
baudRate: 9600,
parser: SerialPort.parsers.readline('\n')
});
port.on('data', function(data) {
console.log("%d Degrees C", data);
});
//
// Source adapted slightly from https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor.
//
//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
}
void loop() // run over and over again
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.println(temperatureC);
delay(2000); //waiting a second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment