Skip to content

Commit

Permalink
added Kalman filter example
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Jan 26, 2019
1 parent 83ccb40 commit 4deb7b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
WifiOfficeSensorsPachube/passwords.h
*.DS_Store
*.fzz
*.vscode*
42 changes: 42 additions & 0 deletions KalmanFilter/KalmanFilter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Kalman filter
Reads an analog sensor on pin A0 and smooths the result with a Kalman filter.
Uses the SimpleKalmanFilter library (https://github.com/denyssene/SimpleKalmanFilter)
Install the library from the Library Manager.
The best way to see the result is to use the Serial Plotter
from the Tools menu in the Arduino IDE (command-shift-L).
Circuit:
Analog sensor attached to pin A0 (use something noisy)
created 19 Jan 2019
by Tom Igoe
*/
#include <SimpleKalmanFilter.h>

// first 2 numbers are the amount of variation,
// third is how fast the measurement moves. recommended: 0.01.
// change these values and see how the result changes.
SimpleKalmanFilter filter(2, 2, 0.01);

int intensity = 0;
int lastEstimate = 0;
int peakValue = 0;
int threshold = 605;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the input:
float sensorValue = analogRead(A0);
Serial.print(sensorValue);
Serial.print(",");

// calculate the estimated value with Kalman Filter
float estimate = filter.updateEstimate(sensorValue);
Serial.println(estimate);
}
2 changes: 1 addition & 1 deletion TMP36/TMP36.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void loop() {
// read the value from the sensor:
int sensorValue = analogRead(A0);
// convert the reading to millivolts:
float voltage = sensorValue * (5.0/ 1024.0);
float voltage = sensorValue * (3.3/ 1024.0);
// convert the millivolts to temperature celsius:
float temperature = (voltage - 0.5)/0.01;
// print the temperature:
Expand Down
19 changes: 2 additions & 17 deletions TMP36Datalogger/TMP36Datalogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ Temperature and Humidity sensors
const int chipSelect = 4; // CS pin for the SD card. Will change depending on your shield or module
boolean cardReady = false;

float voltage = 5.0; // analog reference voltage
float voltage = 1.3; // analog reference voltage
const float resolution = 1024.0; // A-to-D resolution

float tempOffset = 0.5; // temp sensor offset
float tempSlope = 0.01; // temp sensor slope
float rhSlope = 0.0062 * voltage; // humidity sensor slope
float rhOffset = 0.16 * voltage; // humidity sensor offset

void setup() {
Serial.begin(9600);
Expand All @@ -46,8 +44,7 @@ void setup() {
void loop() {
float temperature = tempRead(A0); // get a temperature reading
delay(2); // give the ADC time to settle
float humidity = humidityRead(A1, temperature); // get a humidity reading
writeToCard(temperature, humidity); // write the readings to the SD card
writeToCard(temperature, 0); // write the readings to the SD card
delay(1000); // wait a second
}

Expand All @@ -62,18 +59,6 @@ float tempRead(int pinNumber) { // read the temperature sensor:
}


float humidityRead(int pinNumber,float temp) {
// read the humidity sensor:
int humiditySensor = analogRead(pinNumber);
// convert to voltage:
float humidityVoltage = (humiditySensor/resolution) * voltage;
// convert to relative humidity (rH):
float sensorRH = (humidityVoltage - rhOffset) / rhSlope;
// adjust for temperature:
float trueRH = sensorRH / (1.0546 - (0.00216 * temp));
return trueRH;
}

boolean initializeCard() {
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
Expand Down

0 comments on commit 4deb7b8

Please sign in to comment.