analogRead() can't read the voltage correctly

Hello,

I wanted to measure the voltage across the resistor connected in series with the Emitter (See the attachment). Though an external voltmeter was reading the voltage correctly when changing the duty cycle, however, the A0 pin wasn’t. Following is the code:

[int voltageMeasurePin = A0;
int pwmPin = 9;

float val = 0.0;
float voltage;

void setup() {
pinMode(pwmPin, OUTPUT);

Serial.begin(9600);
}

void loop() {

int value = 255;
analogWrite(pwmPin, value);
val = analogRead(voltageMeasurePin);
voltage = val * (5.04 / 1023.0);
Serial.println(voltage);
delay(1000);

}]

Anyone please help me figuring this one out.

A PWM pin outputs 0-5V nothing in between.
Time to Google PWM.
Do not put any more than 5 volts on you Arduino pins unless you want to damage them!

Analog write produces PWM, not an analog signal. There is no DAC on Arduino boards (other than the Due).

The naming of analogRead() and analogWrite() does little to help matters, considering that the two have about nothing to do with eachother.

Then, how do I read the voltage across resistor connected in series with emitter using arduino uno? Thanks for your reply.

First off, your schematic shows a 6 volt supply. This has a good possibility of damaging your Arduino. Use a five volt power supply instead.

Pin 9 outputs 0-5 pulses only. You could feed pin 9 to a low pass filter.

But what are you trying to achieve?

So the question is… What are you really-really trying to do? There’s no need to monitor the PWM output since your software already knows the PWM value (assuming your circuit is functioning).

Though an external voltmeter was reading the voltage correctly…

If you happen to have an analog meter, this will work every time since the analog meter movement is a kind of motor and it responds to the amount of energy.

In general, a digital multimeter cannot be relied upon to read the average (or RMS) unless it has a “true RMS” feature.

You can smooth-out the PWM with a simple [u]RC Low-Pass Filter[/u]. An R-C time-constant of 1/10th of a second or more (i.e. 100k and 1uF) should do the trick.

anjan_arduino: Then, how do I read the voltage across resistor connected in series with emitter using arduino uno? Thanks for your reply.

with analogRead()

analogRead() is just fine for measuring voltage. It can't measure the duty cycle of a PWM signal without external components to turn that into an analog voltage, at least not directly (can figure it out in software if you take enough samples)

What doesn't work in the example you posted is that you're controlling the transistor with analogWrite() - thus the transistor is turning on and off rapidly, and so the voltage on the emitter is also changing (between 0v and something closer to 6v). If you measure with something with a slow response, like a multimeter, you may see the average voltage (note - not all DMMs give "accurate" results in this situation), whereas the ADC, which takes under a millisecond, will see either the high or low voltage (you might be able to see this if you cranked up the serial baud rate - at 9600, the data will be coming in much faster than the serial can dispose of it.

Then, how do I read the voltage across resistor connected in series with emitter using arduino uno?

The way you're doing it is fine (except you need a voltage divider so you don't apply 6V to the input).

But, you have to understand what you are reading and that you do NOT have a continuously variable DC voltage across that resistor. With PWM you have a voltage that's switching rapidly between ~0V and ~6V.

If you program your Arduino for 50% PWM (128) half of your readings should be zero and half your readings should be 5V.

Why would the voltage ever be anything than 0 with arduino low signal out, or ~6V - Vbe (~0.7) = ~5.3V with arduino high signal out?

CrossRoads: Why would the voltage ever be anything than 0 with arduino low signal out, or ~6V - Vbe (~0.7) = ~5.3V with arduino high signal out?

It could be a transistor with higher drop, or a higher drop at the current in question (that is, Vbe is what he's actually trying to measure).

Or insufficient base current to saturate the transistor

Could be a 6v battery that has high internal resistance, so the voltage falls when it's powering the load resistor.

Why do you need to read the arduino's output with the arduino? Is this really what you are trying to accomplish in the end? If you are trying to read a value from a analog component (e.g. a pot) it should work fine (as long as you don't give the pin an input of over 5v).

Why would the voltage ever be anything than 0 with arduino low signal out, or ~6V - Vbe (~0.7) = ~5.3V with arduino high signal out?

~True
The most it can be is when the base is at Arduino 5 volts:
5VB - .7Vbe = 4.3Vre

The PWM situation still holds true.
.

Collector voltage can be anything, as long as the transistor can withstand it. Emitter is always 0.7v lower than the base. Leo..

Thanks everyone for your valuable comments.

In fact, i need to measure the analog output voltage across the resistor to further manipulate the measured voltage, i.e. multiplying the voltage with the output current in order to measure power.

So, if I connect a capacitor for smoothing purpose across the resistor in series with the Emitter, then I could get an analog output which i can read with analogread(). Would that be okay?

Thanks in advance.

You will have to select/tune the values of the components. If you have a scope you will get a mental image of what is happening. There will be ripple with the filtered PWM.

.

Measuring the voltage across a resistor, is only going to work if one end of the resistor is at ground.

analogRead( ) is not very useful for measuring the voltage between any two points in your circuit, only between one point and ground.

The image in the first post shows the emitter connected to A0 and Arduino GND to 6V GND.

anjan_arduino: So, if I connect a capacitor for smoothing purpose across the resistor in series with the Emitter, then I could get an analog output which i can read with analogread(). Would that be okay?

No, you need a low pass filter such as this: |500x358

For maximum accuracy check the Arduino analogue reading when the 1MΩ resistor is connected to earth instead of to the transistor's emitter (don't short out the emitter resistor!). There may be a small reading due to the Arduino chip's input current which you can offset in your software.

I am assuming pin D9 is not set to generate PWM.

By the way, the 6 volts supply is OK.

Why do you want to measure a resistor this way. Using PWM does not make sense. Or is there more to the story. And why this 6volt source. ??

The original circuit looks more like a battery discharger/tester. An attempt to feed variable DC into the base (missing cap), and measure the voltage over the current sense resistor. But then again, it could be anything right now. Leo..

Thanks everyone for your help. My circuit is working.