Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricImpSampleCode/bb65dc351934a95f2323 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/bb65dc351934a95f2323 to your computer and use it in GitHub Desktop.
Example code to sample a thermistor connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
// Assign hardware.pin5 to a global variable, therm
therm <- hardware.pin5;
// Configure pin5/therm for analog input
therm.configure(ANALOG_IN);
// These constants are particular to the thermistor we're using.
// Check your datasheet for what values you should be using
const B_THERM = 3977.0;
const T0_THERM = 298.15;
// The resistor in the circuit (10KΩ)
const R2 = 10000.0;
function getTemp() {
local vin = hardware.voltage();
local vout = vin * therm.read() / 65535.0;
local rTherm = (R2 * vin / vout) - R2;
local lnTherm = math.log(10000.0 / rTherm);
local tempK = (T0_THERM * B_THERM) / (B_THERM - T0_THERM * lnTherm);
local tempC = tempK - 273.15;
local tempF = tempC * 9.0 / 5.0 + 32.0;
local temp = {};
temp.celsius <- tempC;
temp.fahrenheit <- tempF;
return temp;
}
function poll() {
// Get and log the temperature in both scales
local temp = getTemp();
server.log(format("Temperature: %.2fC, %.2fF", temp.celsius, temp.fahrenheit));
// Wake in 5 seconds and read the value again:
imp.wakeup(5.0, poll);
}
// Call the function to make an inital poll
poll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment