JavaScript ADC demo.
This commit is contained in:
parent
e3053c4d0d
commit
c9c8ecdb95
72
js/adc/ai-sensor.js
Normal file
72
js/adc/ai-sensor.js
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// Script created by ChatGPT 4
|
||||||
|
// @CodeAllNight / MrDerekJamison
|
||||||
|
// Discord server - https://discord.com/invite/NsjCvqwPAd
|
||||||
|
// Github - https://github.com/jamisonderek/flipper-zero-tutorials
|
||||||
|
|
||||||
|
// Flipper Zero JavaScript ADC (Analog-to-Digital Converter) demo.
|
||||||
|
|
||||||
|
// Required hardware connections...
|
||||||
|
// Step 1. Connect a NTC Thermistor (R0:2K, B:3470K) to 3.3V and Pin A7.
|
||||||
|
// Step 2. Connect a 2.2K Resistor to Pin A7 and GND.
|
||||||
|
|
||||||
|
// Requires MNTM-005 (or current Momentum dev build)
|
||||||
|
|
||||||
|
let gpio = require("gpio");
|
||||||
|
let Math = require("math");
|
||||||
|
|
||||||
|
// Constants for the thermistor calculation
|
||||||
|
let B = 3470; // B value of the thermistor
|
||||||
|
let R0 = 2000; // Resistance of the thermistor at T0 (in ohms)
|
||||||
|
let T0 = 298.15; // Reference temperature (25°C in Kelvin)
|
||||||
|
let Vref = 3.3; // Reference voltage in volts
|
||||||
|
|
||||||
|
// Initialize pin A7 as analog
|
||||||
|
gpio.init("PA7", "analog", "no");
|
||||||
|
|
||||||
|
// Start analog reading with a reference voltage of 3.3V
|
||||||
|
gpio.startAnalog(2048); // vRef = 2.048V (scaled)
|
||||||
|
|
||||||
|
function readTemperature() {
|
||||||
|
let millivolts = gpio.readAnalog("PA7");
|
||||||
|
|
||||||
|
// Voltage divider calculation
|
||||||
|
let R1 = 2200; // Known resistor value in ohms (2.2kΩ)
|
||||||
|
let Vout = millivolts / 1000; // Convert millivolts to volts
|
||||||
|
|
||||||
|
if (Vout === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Rt = R1 * (Vref / Vout - 1); // Corrected thermistor resistance calculation
|
||||||
|
|
||||||
|
// Steinhart-Hart equation to calculate temperature in Kelvin
|
||||||
|
let logRt = Math.log(Rt / R0);
|
||||||
|
let invT = (1 / T0) + (1 / B) * logRt;
|
||||||
|
let tempK = 1 / invT;
|
||||||
|
let tempC = tempK - 273.15; // Convert Kelvin to Celsius
|
||||||
|
let tempF = tempC * 9 / 5 + 32; // Convert Celsius to Fahrenheit
|
||||||
|
|
||||||
|
return tempF;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTemperature(temp) {
|
||||||
|
let roundedTemp = Math.floor(temp * 100 + 0.5) / 100; // Round to two decimal places
|
||||||
|
let intPart = Math.floor(roundedTemp); // Integer part
|
||||||
|
let fracPart = Math.floor((roundedTemp - intPart) * 100); // Fractional part
|
||||||
|
|
||||||
|
let intPartStr = to_string(intPart);
|
||||||
|
let fracPartStr = fracPart < 10 ? '0' + to_string(fracPart) : to_string(fracPart); // Ensure two digits in the fractional part
|
||||||
|
|
||||||
|
return intPartStr + '.' + fracPartStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main loop to read and display the temperature every second
|
||||||
|
while (true) {
|
||||||
|
let tempF = readTemperature();
|
||||||
|
if (tempF !== 0) { // Ignore invalid measurements
|
||||||
|
print("Temperature: " + formatTemperature(tempF) + " F"); // Use " F" instead of "°F"
|
||||||
|
}
|
||||||
|
delay(1000); // Wait for 1 second
|
||||||
|
}
|
||||||
|
|
||||||
|
gpio.stopAnalog();
|
67
js/adc/sensor.js
Normal file
67
js/adc/sensor.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// Script created in video https://youtu.be/ETn9E4L6EY0
|
||||||
|
// @CodeAllNight / MrDerekJamison
|
||||||
|
// Discord server - https://discord.com/invite/NsjCvqwPAd
|
||||||
|
// Github - https://github.com/jamisonderek/flipper-zero-tutorials
|
||||||
|
|
||||||
|
// Flipper Zero JavaScript ADC (Analog-to-Digital Converter) demo.
|
||||||
|
|
||||||
|
// Required hardware connections...
|
||||||
|
// Step 1. Connect a NTC Thermistor (R0:2K, B:3470K) to 3.3V and Pin A7.
|
||||||
|
// Step 2. Connect a 2.2K Resistor to Pin A7 and GND.
|
||||||
|
|
||||||
|
// Requires MNTM-005 (or current Momentum dev build)
|
||||||
|
|
||||||
|
let gpio = require("gpio");
|
||||||
|
let Math = require("math");
|
||||||
|
|
||||||
|
gpio.init("PA7", "analog", "no");
|
||||||
|
|
||||||
|
// 2048mV reference voltage
|
||||||
|
gpio.startAnalog(2048);
|
||||||
|
|
||||||
|
function mvToSensorResistance(millivolts) {
|
||||||
|
let Rfixed = 2200; // 2200 ohm fixed resistor
|
||||||
|
let Vtotal = 3.3; // resistor + sensor series connected to 3.3
|
||||||
|
let Vrfixed = millivolts / 1000; // millivolts to volts
|
||||||
|
let I = Vrfixed / Rfixed;
|
||||||
|
let Rtotal = Vtotal / I;
|
||||||
|
let Rsensor = Rtotal - Rfixed;
|
||||||
|
return Rsensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resistanceToCelsius(rsensor) {
|
||||||
|
// B value of the thermistor
|
||||||
|
let B = 3470;
|
||||||
|
// Reference temp (25°C in Kelvin)
|
||||||
|
let T0 = 273.15 + 25;
|
||||||
|
// Resistance of the thermistor at T0 (in ohms)
|
||||||
|
let R0 = 2000;
|
||||||
|
// B or β parameter equation
|
||||||
|
// from https://en.wikipedia.org/wiki/Thermistor
|
||||||
|
let invT = (1 / T0) + (1 / B) * Math.log(rsensor/R0);
|
||||||
|
let tempK = 1 / invT;
|
||||||
|
// Convert Kelvin to Celsius
|
||||||
|
let tempC = tempK - 273.15;
|
||||||
|
return tempC;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to print 1 digits after decimal written by ChatGPT 4o.
|
||||||
|
function formatTemperature(temp) {
|
||||||
|
let roundedTemp = Math.floor(temp * 10 + 0.5) / 10; // Round to one decimal place
|
||||||
|
let intPart = Math.floor(roundedTemp); // Integer part
|
||||||
|
let fracPart = Math.floor((roundedTemp - intPart) * 10); // Fractional part
|
||||||
|
return to_string(intPart) + '.' + to_string(fracPart);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i=0; i<50; i++) {
|
||||||
|
let mv = gpio.readAnalog("PA7");
|
||||||
|
let sensor_resistance = mvToSensorResistance(mv);
|
||||||
|
let tempC = resistanceToCelsius(sensor_resistance);
|
||||||
|
let msg = to_string(mv) + " mV:";
|
||||||
|
msg = msg + to_string(sensor_resistance) + " Ohms:";
|
||||||
|
msg = msg + formatTemperature(tempC) + " C";
|
||||||
|
print(msg);
|
||||||
|
delay(1000); // 1 second
|
||||||
|
}
|
||||||
|
|
||||||
|
gpio.stopAnalog();
|
Loading…
x
Reference in New Issue
Block a user