Show Posts
|
|
Pages: 1 2 [3] 4 5 ... 13
|
|
31
|
Using Arduino / Programming Questions / Re: amp | watt hour meter
|
on: May 12, 2012, 03:51:48 pm
|
New updates, including LCD, and improved voltage divider fixing an impedance mismatch. Also showcased on http://www.instructables.com/id/DIY-Amp-Hour-Meter-Arduino/#include <LiquidCrystal.h>
/* This sketch describes how to connect a ACS715 Current Sense Carrier (http://www.pololu.com/catalog/product/1186) to the Arduino, and read current flowing through the sensor.
*/
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
/*
Vcc on carrier board to Arduino +5v GND on carrier board to Arduino GND OUT on carrier board to Arduino A0
Insert the power lugs into the loads positive lead circuit, arrow on carrier board points to load, other lug connects to power supply positive
*/ int batMonPin = A4; // input pin for the voltage divider int batVal = 0; // variable for the A/D value float pinVoltage = 0; // variable to hold the calculated voltage float batteryVoltage = 0; float ratio = 2.4; // Change this to match the MEASURED ration of the circuit, 12k R1 and 5k R2 int analogInPin = A0; // Analog input pin that the carrier board OUT is connected to int sensorValue = 0; // value read from the carrier board int outputValue = 0; // output in milliamps unsigned long msec = 0; float time = 0.0; int sample = 0; float totalCharge = 0.0; float averageAmps = 0.0; float ampSeconds = 0.0; float ampHours = 0.0; float wattHours = 0.0; float amps = 0.0;
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); lcd.begin(20, 4); }
void loop() {
// read the analog in value: sensorValue = analogRead(analogInPin); // convert to milli amps outputValue = (((long)sensorValue * 5000 / 1024) - 500 ) * 1000 / 133; /* sensor outputs about 100 at rest. Analog read produces a value of 0-1023, equating to 0v to 5v. "((long)sensorValue * 5000 / 1024)" is the voltage on the sensor's output in millivolts. There's a 500mv offset to subtract. The unit produces 133mv per amp of current, so divide by 0.133 to convert mv to ma */ batVal = analogRead(batMonPin); // read the voltage on the divider pinVoltage = batVal * 0.00488; // Calculate the voltage on the A/D pin // A reading of 1 for the A/D = 0.0048mV // if we multiply the A/D reading by 0.00488 then // we get the voltage on the pin.
batteryVoltage = pinVoltage * ratio; // Use the ratio calculated for the voltage divider // to calculate the battery voltage amps = (float) outputValue / 1000; float watts = amps * batteryVoltage; Serial.print("Volts = " ); Serial.print(batteryVoltage); Serial.print("\t Current (amps) = "); Serial.print(amps); Serial.print("\t Power (Watts) = "); Serial.print(watts); sample = sample + 1; msec = millis(); time = (float) msec / 1000.0; totalCharge = totalCharge + amps; averageAmps = totalCharge / sample; ampSeconds = averageAmps*time;
ampHours = ampSeconds/3600; wattHours = batteryVoltage * ampHours;
Serial.print("\t Time (hours) = "); Serial.print(time/3600); Serial.print("\t Amp Hours (ah) = "); Serial.print(ampHours); Serial.print("\t Watt Hours (wh) = "); Serial.println(wattHours);
lcd.setCursor(0,0); lcd.print(batteryVoltage); lcd.print(" V "); lcd.print(amps); lcd.print(" A "); lcd.setCursor(0,1); lcd.print(watts); lcd.print(" W "); lcd.print(time/3600); lcd.print(" H "); lcd.setCursor(0,2); lcd.print(ampHours); lcd.print(" Ah "); lcd.print(wattHours); lcd.print(" Wh "); // wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }
|
|
|
|
|
32
|
Using Arduino / Programming Questions / Re: amp | watt hour meter
|
on: May 05, 2012, 01:46:17 pm
|
Thanks for the help. Got it working the way I wanted it. See attached: /* This sketch describes how to connect a ACS715 Current Sense Carrier (http://www.pololu.com/catalog/product/1186) to the Arduino, and read current flowing through the sensor.
Vcc on carrier board to Arduino +5v GND on carrier board to Arduino GND OUT on carrier board to Arduino A0
Insert the power lugs into the loads positive lead circuit, arrow on carrier board points to load, other lug connects to power supply positive
*/ int batMonPin = A4; // input pin for the voltage divider int batVal = 0; // variable for the A/D value float pinVoltage = 0; // variable to hold the calculated voltage float batteryVoltage = 0; float ratio = 3.2; // Change this to match the MEASURED ration of the circuit int analogInPin = A0; // Analog input pin that the carrier board OUT is connected to int sensorValue = 0; // value read from the carrier board int outputValue = 0; // output in milliamps unsigned long msec = 0; float time = 0.0; int sample = 0; float totalCharge = 0.0; float averageAmps = 0.0; unsigned long hours = 0; unsigned long minutes = 0; float ampHours = 0.0; float wattHours = 0.0; float amps = 0.0;
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }
void loop() {
// read the analog in value: sensorValue = analogRead(analogInPin); // convert to milli amps outputValue = (((long)sensorValue * 5000 / 1024) - 500 ) * 1000 / 133; /* sensor outputs about 100 at rest. Analog read produces a value of 0-1023, equating to 0v to 5v. "((long)sensorValue * 5000 / 1024)" is the voltage on the sensor's output in millivolts. There's a 500mv offset to subtract. The unit produces 133mv per amp of current, so divide by 0.133 to convert mv to ma */ batVal = analogRead(batMonPin); // read the voltage on the divider pinVoltage = batVal * 0.00488; // Calculate the voltage on the A/D pin // A reading of 1 for the A/D = 0.0048mV // if we multiply the A/D reading by 0.00488 then // we get the voltage on the pin.
batteryVoltage = pinVoltage * ratio; // Use the ratio calculated for the voltage divider // to calculate the battery voltage // print the results to the serial monitor: amps = (float) outputValue / 1000; Serial.print("Volts = " ); Serial.print(batteryVoltage); Serial.print("\t Current (amps) = "); Serial.print(amps); Serial.print("\t Power (Watts) = "); float watts = amps * batteryVoltage; Serial.print(watts); sample = sample + 1; msec = millis(); totalCharge = totalCharge + amps; minutes = msec/1000/60; time = (float) minutes / 60; ampHours = totalCharge/time; averageAmps = totalCharge / sample; ampHours = averageAmps*time; wattHours = batteryVoltage * ampHours;
Serial.print("\t Time (hours) = "); Serial.print(time); Serial.print("\t Amp Hours (ah) = "); Serial.print(ampHours); Serial.print("\t Watt Hours (wh) = "); Serial.println(wattHours);
// wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }
|
|
|
|
|
34
|
Using Arduino / Programming Questions / amp | watt hour meter
|
on: May 05, 2012, 08:07:11 am
|
I've built a voltage and current monitor using a ACS715 from Pololu. I'm calculating hours using millis, and total amp hours consumed. millis is a unsigned long, but I'd like to see hours in operation in a format like 1.25 hours, not jumps from 0 - 1 - 2 etc. Photo's, schematics etc. posted at http://tech.groups.yahoo.com/group/arduinohome/. /* This sketch describes how to connect a ACS715 Current Sense Carrier (http://www.pololu.com/catalog/product/1186) to the Arduino, and read current flowing through the sensor.
Vcc on carrier board to Arduino +5v GND on carrier board to Arduino GND OUT on carrier board to Arduino A0
Insert the power lugs into the loads positive lead circuit, arrow on carrier board points to load, other lug connects to power supply positive
*/ int batMonPin = A4; // input pin for the voltage divider int batVal = 0; // variable for the A/D value float pinVoltage = 0; // variable to hold the calculated voltage float batteryVoltage = 0; float ratio = 3.2; // Change this to match the MEASURED ration of the circuit int analogInPin = A0; // Analog input pin that the carrier board OUT is connected to int sensorValue = 0; // value read from the carrier board int outputValue = 0; // output in milliamps unsigned long time = 0; int sample = 0; float totalAmps = 0.0; float averageAmps = 0.0; unsigned long hours = 0; float ampHours = 0.0;
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }
void loop() { // increment sample counter sample = ++sample; time = millis(); // read the analog in value: sensorValue = analogRead(analogInPin); // convert to milli amps outputValue = (((long)sensorValue * 5000 / 1024) - 500 ) * 1000 / 133; /* sensor outputs about 100 at rest. Analog read produces a value of 0-1023, equating to 0v to 5v. "((long)sensorValue * 5000 / 1024)" is the voltage on the sensor's output in millivolts. There's a 500mv offset to subtract. The unit produces 133mv per amp of current, so divide by 0.133 to convert mv to ma */ batVal = analogRead(batMonPin); // read the voltage on the divider pinVoltage = batVal * 0.00488; /* Calculate the voltage on the A/D pin A reading of 1 for the A/D = 0.0048mV if we multiply the A/D reading by 0.00488 then we get the voltage on the pin. */
batteryVoltage = pinVoltage * ratio; // Use the ratio calculated for the voltage divider // to calculate the battery voltage // print the results to the serial monitor: Serial.print("Voltage = " ); Serial.print(batteryVoltage); Serial.print("\t Current (ma) = "); Serial.print(outputValue); Serial.print("\t Power (Watts) = "); float amps = outputValue / 1000.0; float watts = amps * batteryVoltage; Serial.print(watts); totalAmps = totalAmps + amps; averageAmps = totalAmps / sample; hours = time/3600000; ampHours = averageAmps * hours;
Serial.print("\t Time (hours) = "); Serial.print(hours); Serial.print("\t Energy (ah) = "); Serial.println(ampHours); // wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }
|
|
|
|
|
39
|
Using Arduino / Motors, Mechanics, and Power / Servo's Freezing
|
on: March 04, 2012, 10:17:15 am
|
I have a hxt900 servo, and when using both example sketches included in Arduino 1.0, the servo (and a second identical one) freeze, quit, operate erratically, etc. Checked the wiring to make sure it's solid, and correct. I also checked to see the pot was being read properly. The "L" light on my 2560 lights at the same time the servo "shifts" 2-5 degrees or so (every 9 seconds) Anyone else having similar issues? // Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { Serial.begin(9600); // setup serial myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) // Serial.println(val); val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value // Serial.println(val); delay(15); // waits for the servo to get there }
|
|
|
|
|
41
|
Using Arduino / Programming Questions / Re: Averaging Samples
|
on: January 18, 2012, 11:41:59 am
|
I got it. I merged the two sections so buttonState would increment before each reading. Finished, working, I'm pleased with myself, LOL. // Take and display averages
void average(){ avgVal = digitalRead(avgPin); // read input value and store it in val delay(25); // 10 milliseconds is a good amount of time avgVal2 = digitalRead(avgPin); // read the input again to check for bounces if (avgVal == avgVal2) { // make sure we got 2 consistant readings! if (avgVal != avgButtonState) { // the button state has changed! if (avgVal == LOW) { // check if the button is pressed if (sampleState == 0) { // No samples sampleState++; // Take first sample m1=0; m2=0; m3=0; sendText(04, 19, 03, 255, 255, " "); sendText(14, 19, 03, 255, 255, " "); sendText(24, 19, 03, 255, 255, " "); sendText(34, 19, 03, 255, 255, " "); } else { if (sampleState == 1) { // sampleState++; // Take second sample m1=num; // convert m1 to samplel1 and display samplel1=convert(); sendData(04, 19, 03, 255, 255, samplel1); } else { if (sampleState == 2) { // sampleState++; // Take third sample m2=num; // convert m2 to samplel2 and display samplel2=convert(); sendData(14, 19, 03, 255, 255, samplel2);
} else { if (sampleState == 3) { // sampleState = 0; // Reset samples m3=num; // convert m3 to samplel3 and display samplel3=convert(); sendData(24, 19, 03, 255, 255, samplel3); num=(m1+m2+m3)/3; // convert mAvg to sampleAvgl and display sampleAvgl=convert(); sendData(34, 19, 03, 255, 255, sampleAvgl);
} } } } } } avgButtonState = avgVal; // save the new state in our variable }
|
|
|
|
|
42
|
Using Arduino / Programming Questions / Re: Averaging Samples
|
on: January 18, 2012, 10:33:45 am
|
here's my final code, but it doesn't seem to work. The 3 samples display, and average, but are not "fixed". As the micrometer moves, the last sample taken keeps updating. When a sample is taken, sampleState should increment by one, so that section of code should no longer execute. The complete sketch is in the attached file: // Take and display averages
void average(){ avgVal = digitalRead(avgPin); // read input value and store it in val delay(25); // 10 milliseconds is a good amount of time avgVal2 = digitalRead(avgPin); // read the input again to check for bounces if (avgVal == avgVal2) { // make sure we got 2 consistant readings! if (avgVal != avgButtonState) { // the button state has changed! if (avgVal == LOW) { // check if the button is pressed if (sampleState == 0) { // No samples sampleState = 1; // Take first sample } else { if (sampleState == 1) { // sampleState = 2; // Take second sample } else { if (sampleState == 2) { // sampleState = 3; // Take third sample } else { if (sampleState == 3) { // sampleState = 0; // Reset samples } } } } } } avgButtonState = avgVal; // save the new state in our variable }
// Now do whatever the sampleState indicates if (sampleState == 0) { // all-off m1=0; m2=0; m3=0; sendText(04, 19, 03, 255, 255, " "); sendText(14, 19, 03, 255, 255, " "); sendText(24, 19, 03, 255, 255, " "); sendText(34, 19, 03, 255, 255, " "); }
if (sampleState == 1) { // Sample 1 m1=num; // convert m1 to samplel1 and display samplel1=convert(); sendData(04, 19, 03, 255, 255, samplel1); }
if (sampleState == 2) { // Sample 2 m2=num; // convert m2 to samplel2 and display samplel2=convert(); sendData(14, 19, 03, 255, 255, samplel2); } if (sampleState == 3) { // Sample 3 m3=num; // convert m3 to samplel3 and display samplel3=convert(); sendData(24, 19, 03, 255, 255, samplel3); num=(m1+m2+m3)/3; // convert mAvg to sampleAvgl and display sampleAvgl=convert(); sendData(34, 19, 03, 255, 255, sampleAvgl); } }
|
|
|
|
|
43
|
Using Arduino / Programming Questions / Re: Averaging Samples
|
on: January 17, 2012, 11:36:01 am
|
Here is a separate sketch I was playing with to see how it might be constructed. I don't have live numbers coming in on mm, nor am I converting to part numbers, this just averages three canned numbers as a test: // this constant won't change: const int avgButtonPin = 6; // connected to ground through a N.O. momentary, and to +5 through a 10k resistor.
// Variables will change: int avgButton = 0; // counter for the number of button presses int avgButtonState = 0; // current state of the button int avgLastButtonState = 0; // previous state of the button int m1 = 0; int m2 = 0; int m3 = 0; int mm = 0; int mAvg = 0;
void setup() { // initialize the button pin as a input: pinMode(avgButtonPin, INPUT); Serial.begin(9600); }
void loop() { // read the pushbutton input pin: avgButtonState = digitalRead(avgButtonPin);
// compare the buttonState to its previous state if (avgButtonState != avgLastButtonState) { // if the state has changed, increment the counter if (avgButtonState == HIGH) { // if the current state is LOW then the button // went from off to on: if (avgButton == 1) { // m1 = mm; m1=100; avgButton++; } if (avgButton == 2) { // m2 = mm; m2=150; avgButton++; } if (avgButton == 3) { // m3 = mm; m3=155; avgButton++; } if (avgButton == 4) { m1 = 0; m2 = 0; m3 = 0; avgButton = 0; } mAvg=(m1+m2+m3)/3; Serial.print(avgButton); Serial.print(m1); Serial.print(m2); Serial.print(m3); Serial.println(mAvg); } delay(500); } /*
sendDesc(15, 03, 03, 255, 255, "Reading #1"); sendDesc(15, 03, 03, 255, 255, "Reading #2"); sendDesc(15, 03, 03, 255, 255, "Reading #3"); sendDesc(15, 03, 03, 255, 255, "Average"); sendData(04, 03, 03, 255, 255, m1); sendData(04, 03, 03, 255, 255, m2); sendData(04, 03, 03, 255, 255, m3); sendData(04, 03, 03, 255, 255, mAvg); */ }
|
|
|
|
|
44
|
Using Arduino / Displays / Re: 4D uVGA-II SGC Issues
|
on: January 17, 2012, 10:59:34 am
|
vgaBaud(7); was the call to the routine that changes the baud rate of the vga card. The uVGA-II does not support autobaud, and must be communicated with at 9600 before sending a new baud rate. I did resolve the issue, however. I built the following routine: // check ack / nack byte nacAck() { byte b=0x00; // 0x06; while (!(Serial3.available())) { delay(2); } b = Serial3.read(); return b; } and place a call at the end of each command sequence: //opaque text
Serial3.print("O");
Serial3.print(byte(01));
a = nacAck();
|
|
|
|
|