Measure DC Voltage ...
 
Notifications
Clear all

Measure DC Voltage and Current with Arduino

38 Posts
15 Users
19 Likes
4,136 Views
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 1 year ago
Posts: 338
 
Posted by: @abdulhak33m

Thank you Bill.

I modified this sketch to control a battery charger so it can give a high signal to a gren led when battery voltage is above 13.8V and as well switch off charging via a relay module. 

its not working. please i appreciate any help

// Define analog input
#define ANALOG_IN_PIN A0

// Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;

// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;

// Float for Reference Voltage
float ref_voltage = 5.0;

// Integer for ADC value
int adc_value = 0;

// other parameters for battery control and user interfaces

int redled = 13;
int greenled = 12;
int blueled = 8;
int cutoffhigh = 7;
int cutofflow = 4;

void setup(){
// Setup Serial Monitor
Serial.begin(115200);

Serial.println("DC Voltage Test");
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
pinMode(cutoffhigh, OUTPUT);
pinMode(cutofflow, OUTPUT);

}

void loop(){
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);

// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;

// Calculate voltage at divider input
in_voltage = adc_voltage / (R2/(R1+R2));

// Print results to Serial Monitor to 2 decimal places
Serial.print("Input Voltage = ");
Serial.println(in_voltage, 2);

// Short delay
delay(500);

if( in_voltage >=0.00 && in_voltage <=10.00){
digitalWrite(cutofflow, HIGH);
digitalWrite(cutoffhigh, LOW);

digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
}

else if(in_voltage >=10.10 && in_voltage <= 10.80){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);

}

else if( in_voltage >10.81 && in_voltage <= 12.29 ){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, HIGH);

}

else if (in_voltage >= 12.30 && in_voltage <= 12.49){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);

digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
digitalWrite(blueled, LOW);

}

else if (in_voltage >12.50 && in_voltage >=12.6){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, HIGH);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);

}

else {
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);

}

}

Hello @abdulhak33m , Can you be a little more specific on what's not working?

I notice in your code the "if" statements (contain characters like '&gt;' and the like.. could this be the source of your "it's not working"..I think the compiler doesn't recognize those as a substitute for "<=" or >=" in your code.. try changing them using appropriate symbols ..

let us know how it goes..

regards,

LouisR

LouisR


   
ReplyQuote
abdulhak33m
(@abdulhak33m)
Active Member
Joined: 2 years ago
Posts: 10
 

@inst-tech Thank you so much for the response. I reviewed the code and removed all <= clauses and it worked exactly the way i wished it would. the red led comes on when battery voltage is below 10v and the cutofflow pin disconnects the load from the battery. Similarly, the green light turn on when the battery is full at 12.6V and the cutoffhigh pin disconnects the charger from its power source via a relay. I only want to but a LM4040 reference voltage module to increase the accuracy of readings from the voltage sensor. I saw online that in the absence of an LM4040 module; i could construct a similar voltage reference set up using the LM4040 IC and a shunt resistor. 

Thank you so much for the response


   
Inst-Tech reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 1 year ago
Posts: 338
 
Posted by: @abdulhak33m

@inst-tech Thank you so much for the response. I reviewed the code and removed all <= clauses and it worked exactly the way i wished it would. the red led comes on when battery voltage is below 10v and the cutofflow pin disconnects the load from the battery. Similarly, the green light turn on when the battery is full at 12.6V and the cutoffhigh pin disconnects the charger from its power source via a relay. I only want to but a LM4040 reference voltage module to increase the accuracy of readings from the voltage sensor. I saw online that in the absence of an LM4040 module; i could construct a similar voltage reference set up using the LM4040 IC and a shunt resistor. 

Thank you so much for the response

Your very welcome..your plan to use a LM4040  voltage reference module will most certainly give you better resolution, but you must also set the program code to analogReference(EXTERNAL)..see the following example:

WARNING!
When using AREF, always specify analogReference(EXTERNAL); before doing an analog read, as you could short the internal reference, damaging the Arduino. I recommend you upload this sketch before connecting the AREF pin.

#define aref_voltage 4.096

int ADCPin = 1; //using A1 input for this sketch
int ADCReading;

void setup(){

Serial.begin(9600);

analogReference(EXTERNAL);

}

As I don't assume anything, I thought it best to include this for information purposes only as I don't know what level of understanding you may or may not have.

Good luck with your project.

regards,

LouisR

 

LouisR


   
abdulhak33m reacted
ReplyQuote
AlexTheProgrammer
(@alextheprogrammer)
Eminent Member
Joined: 1 month ago
Posts: 19
 

A question about INA219. Out of the box it's limited to 3A but as I'm building a variation of DB1 robot, I wanna measure current and voltage on the 12v line out of my power supply where I expect up to 30Amp to be pulled, or maybe even more. Do I just need to change that resistor and it would be ok? Or the traces on the sensor board would be fried? or there'll be too significant of a voltage drop?

Also, I'm going to have a 30A fuse between the load(s) and the power supply. Would it be better to have the INA219 between the fuse and the load or between the power supply and the fuse? 

 

The use case is that that I'd have a lot more motors there and the total current that could be pulled could be way north of 30A but my power supply can only produce 51A and I don't really wanna pull more than 30 out of it. So, the current value of current would be broadcasted via I2C and I will programmatically prevent any additional concurrent movements if the current value of current is close to 30A or even have to send a stop interrupt if it's out of control (e.g. robot is going up the hill with too heavy load).  


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3462
 

@alextheprogrammer 30A is a LOT! I would google trace formulas to determine what is needed. From what I read INA219 are hard to find so until you know what you are getting and what the specs are I wouldn't hazard a guess.

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
AlexTheProgrammer
(@alextheprogrammer)
Eminent Member
Joined: 1 month ago
Posts: 19
 

@zander or do you think there might be a better way of programmatically measuring higher motor voltages to control the system than INA219?


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3462
 

@alextheprogrammer Sorry, not my area of interest or expertise.

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
DaveE
(@davee)
Prominent Member
Joined: 2 years ago
Posts: 714
 

Hi @alextheprogrammer,

   Upgrading a current sensor from 3A to 30A needs a complete redesign to carry a current that high ... not just a resistor change.

--------------

As your hardware experience is clearly limited, I recommend you start by doing some experiments with handling these larger currents ... be aware that large currrents can be dangerous, even at low voltage!

-------------

Generally a fuse is placed as close as possible to the power source, so that the fuse protects against faults over the maximum part of the system   ... as a fuse  can only cover faults on components that are 'downstream' of the fuse.

------------

re: or do you think there might be a better way of programmatically measuring higher motor voltages to control the system than INA219?

I thought the INA219 was being used to measure current, not voltage?

If you meant current, then there are specialist current sensor devices ... companies like Melexis and Allegro make sensors based on the Hall Effect which might be more suitable

... and there appear to be products in the markeplace like:

https://www.aliexpress.com/item/32807039010.html

but I have NO idea if they are any good!

--------------------

But most importantly ... be careful ... high currents can really bite!

Best wishes, Dave


   
ReplyQuote
Page 3 / 3