Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 15, 2016
0 parents commit 9207358
Show file tree
Hide file tree
Showing 111 changed files with 12,720 additions and 0 deletions.
Binary file added 9781430236238.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
122 changes: 122 additions & 0 deletions Arduino Projects Save World/978-1-4302-3623-8_CH07/L7_1/_7_1.pde
@@ -0,0 +1,122 @@
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 6, 7, 8, 9);

const int voltageSensor = A0;
const int currentSensor = A1;

const int numberOfSamples = 3000;

// Calibration constants
const float AC_WALL_VOLTAGE = 120.9;
const float AC_ADAPTER_VOLTAGE = 14.1;
const float AC_VOLTAGE_DIV_VOUT = 0.85;
const float CT_BURDEN_RESISTOR = 40.2;
const float CT_TURNS = 2280.0;

// Calibration coefficients
const float VCAL = 1.0;
const float ICAL = 1.0;
const float PHASECAL = 0.9;

// Calculated ratio constants, modified by VCAL/ICAL
const float AC_ADAPTER_RATIO = AC_WALL_VOLTAGE / AC_ADAPTER_VOLTAGE;
const float AC_VOLTAGE_DIV_RATIO = AC_ADAPTER_VOLTAGE / AC_VOLTAGE_DIV_VOUT;
const float V_RATIO = AC_ADAPTER_RATIO * AC_VOLTAGE_DIV_RATIO * 5 / 1024 * VCAL;
const float I_RATIO = CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL;

// Sample variables
int lastSampleV, lastSampleI, sampleV, sampleI;

// Filter variables
float lastFilteredV, lastFilteredI, filteredV, filteredI;

// Power sample totals
float sumI, sumV, sumP;

// Phase calibrated instantaneous voltage
float calibratedV;

// Calculated power variables
float realPower, apparentPower, powerFactor, voltageRMS, currentRMS;
unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

void setup() {
lcd.begin(16,2);
}

void loop() {
calculatePower();
displayPower();
}

void calculatePower() {
for (int i = 0; i < numberOfSamples; i++) {
// Used for voltage offset removal
lastSampleV = sampleV;
lastSampleI = sampleI;

// Read voltage and current values
sampleV = analogRead(voltageSensor);
sampleI = analogRead(currentSensor);

// Used for voltage offset removal
lastFilteredV = filteredV;
lastFilteredI = filteredI;

// Digital high pass filters to remove 2.5V DC offset
filteredV = 0.996 * (lastFilteredV + sampleV - lastSampleV);
filteredI = 0.996 * (lastFilteredI + sampleI - lastSampleI);

// Phase calibration
calibratedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);

// Root-mean-square voltage
sumV += calibratedV * calibratedV;

// Root-mean-square current
sumI += filteredI * filteredI;

// Instantaneous Power
sumP += abs(calibratedV * filteredI);
}

// Calculation of the root of the mean of the voltage and current squared (rms)
// Calibration coeficients applied
voltageRMS = V_RATIO * sqrt(sumV / numberOfSamples);
currentRMS = I_RATIO * sqrt(sumI / numberOfSamples);

// Calculate power values
realPower = V_RATIO * I_RATIO * sumP / numberOfSamples;
apparentPower = voltageRMS * currentRMS;
powerFactor = realPower / apparentPower;

// Calculate running total kilowatt hours
// This value will reset in 50 days
last_kWhTime = kWhTime;
kWhTime = millis();
// Convert watts into kilowatts and multiply by the time since the last reading in ms
kilowattHour += (realPower / 1000) * ((kWhTime - last_kWhTime) / 3600000.0);

// Reset sample totals
sumV = 0;
sumI = 0;
sumP = 0;
}

void displayPower() {
lcd.clear();
lcd.print(realPower, 0);
lcd.print("w ");
lcd.print(apparentPower, 0);
lcd.print("va ");
lcd.print(powerFactor * 100, 0);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print(voltageRMS, 0);
lcd.print("v ");
lcd.print(currentRMS, 1);
lcd.print("a ");
lcd.print(kilowattHour, 4);
}

170 changes: 170 additions & 0 deletions Arduino Projects Save World/978-1-4302-3623-8_CH07/L7_2/_7_2.pde
@@ -0,0 +1,170 @@
#include <LiquidCrystal.h>
#include <SD.h>

LiquidCrystal lcd(3, 5, 6, 7, 8, 9);
File file;

const int voltageSensor = A0;
const int currentSensor = A1;
const int SDcs = 4;

const int numberOfSamples = 3000;

// Calibration constants
const float AC_WALL_VOLTAGE = 120.9;
const float AC_ADAPTER_VOLTAGE = 14.1;
const float AC_VOLTAGE_DIV_VOUT = 0.85;
const float CT_BURDEN_RESISTOR = 40.2;
const float CT_TURNS = 2280.0;

// Calibration coeficients
const float VCAL = 1.0;
const float ICAL = 1.0;
const float PHASECAL = 0.9;

// Calculated ratio constants, modified by VCAL/ICAL
const float AC_ADAPTER_RATIO = AC_WALL_VOLTAGE / AC_ADAPTER_VOLTAGE;
const float AC_VOLTAGE_DIV_RATIO = AC_ADAPTER_VOLTAGE / AC_VOLTAGE_DIV_VOUT;
const float V_RATIO = AC_ADAPTER_RATIO * AC_VOLTAGE_DIV_RATIO * 5 / 1024 * VCAL;
const float I_RATIO = CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL;

// Sample variables
int lastSampleV, lastSampleI, sampleV, sampleI;

// Filter variables
float lastFilteredV, lastFilteredI, filteredV, filteredI;

// Power sample totals
float sumI, sumV, sumP;

// Phase calibrated instantaneous voltage
float calibratedV;

// Calculated power variables
float realPower, apparentPower, powerFactor, voltageRMS, currentRMS;
unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

unsigned long startTime = 0;
unsigned long interval = 10000;

void setup() {
lcd.begin(16,2);
Serial.begin(9600);

// Starts the SD card
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);

// Checks that the SD card is present
if (!SD.begin(SDcs)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}

void loop() {
calculatePower();
displayPower();

// Opens file on SD if interval has passed
if (startTime + interval < millis()) {
// Opens files and sets to write mode
file = SD.open("data.txt", FILE_WRITE);

// Writes values to file if present
if (file) {
Serial.print("Writing to data.txt... ");

file.print(realPower);
file.print(",");
file.print(apparentPower);
file.print(",");
file.print(powerFactor * 100);
file.print(",");
file.print(voltageRMS);
file.print(",");
file.print(currentRMS);
file.print(",");
file.println(kilowattHour);

Serial.println("done.");
// Need to close a file to save the data
file.close();
Serial.println("File closed.");
} else Serial.println("Error opening data.txt.");
startTime = millis();
}
}

void calculatePower() {
for (int i = 0; i < numberOfSamples; i++) {
// Used for voltage offset removal
lastSampleV = sampleV;
lastSampleI = sampleI;

// Read voltage and current values
sampleV = analogRead(voltageSensor);
sampleI = analogRead(currentSensor);

// Used for voltage offset removal
lastFilteredV = filteredV;
lastFilteredI = filteredI;

// Digital high pass filters to remove 2.5V DC offset
filteredV = 0.996 * (lastFilteredV + sampleV - lastSampleV);
filteredI = 0.996 * (lastFilteredI + sampleI - lastSampleI);

// Phase calibration
calibratedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);

// Root-mean-square voltage
sumV += calibratedV * calibratedV;

// Root-mean-square current
sumI += filteredI * filteredI;

// Instantaneous Power
sumP += abs(calibratedV * filteredI);
}

// Calculation of the root of the mean of the voltage and current squared (rms)
// Calibration coeficients applied
voltageRMS = V_RATIO * sqrt(sumV / numberOfSamples);
currentRMS = I_RATIO * sqrt(sumI / numberOfSamples);

// Calculate power values
realPower = V_RATIO * I_RATIO * sumP / numberOfSamples;
apparentPower = voltageRMS * currentRMS;
powerFactor = realPower / apparentPower;

// Calculate running total kilowatt hours
// This value will reset in 50 days
last_kWhTime = kWhTime;
kWhTime = millis();
// Convert watts into kilowatts and multiply by the time since the last reading
kilowattHour += (realPower / 1000) * ((kWhTime - last_kWhTime) / 3600000.0);

// Reset sample totals
sumV = 0;
sumI = 0;
sumP = 0;
}

void displayPower() {
lcd.clear();
lcd.print(realPower, 0);
lcd.print("w ");
lcd.print(apparentPower, 0);
lcd.print("va ");
lcd.print(powerFactor * 100, 0);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print(voltageRMS, 0);
lcd.print("v ");
lcd.print(currentRMS, 1);
lcd.print("a ");
lcd.print(kilowattHour, 4);
}

Binary file not shown.
@@ -0,0 +1,20 @@
int TSensor = 0; // temperature sensor ADC input pin
int val = 0; // variable to store ADC value read
int TempOffset = 500; // value in mV when ambient is 0 degrees C
int TempCoef = 10; // Temperature coefficient mV per Degree C
float ADCmV = 4.8828; // mV per ADC increment (5 volts / 1024 increments)
float Temp = 0; // calculated temperature in C (accuraccy to two decimal places)

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

void loop()
{
val = analogRead(TSensor); // read the input pin
Temp = ((val * ADCmV) - TempOffset) / TempCoef; // the ADC to C equation
Serial.println(Temp); // display in the SerialMonitor
delay (200);
}

@@ -0,0 +1,52 @@
/*
SpiderTemps 6 sensor
Arduino projects to save the world
This sketch reads all six analog inputs, calculates temperature(C) and outputs them to the serial monitor.
*/

int ADC0, ADC1, ADC2, ADC3, ADC4, ADC5;
int MCPoffset = 500;
int LM35offset = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
getADC();
float temp0 = calcTemp(ADC0, LM35offset);
float temp1 = calcTemp(ADC1, LM35offset);
float temp2 = calcTemp(ADC2, MCPoffset);
float temp3 = calcTemp(ADC3, MCPoffset);
float temp4 = calcTemp(ADC4, MCPoffset);
float temp5 = calcTemp(ADC5, MCPoffset);

Serial.print(temp0, 0);
Serial.print(" ");
Serial.print(temp1, 0);
Serial.print(" ");
Serial.print(temp2, 0);
Serial.print(" ");
Serial.print(temp3, 0);
Serial.print(" ");
Serial.print(temp4, 0);
Serial.print(" ");
Serial.println(temp5, 0);

delay(500);
}

void getADC() {
ADC0 = analogRead(A0);
ADC1 = analogRead(A1);
ADC2 = analogRead(A2);
ADC3 = analogRead(A3);
ADC4 = analogRead(A4);
ADC5 = analogRead(A5);
}

float calcTemp (int val, int offset) {
return ((val * 4.8828) - offset) / 10;
}

0 comments on commit 9207358

Please sign in to comment.