analog read

so i have analog pin set up to analogRead (using readVoltage in matlab) a voltage. the voltage is coming from pin 3 (code: writePWMVoltage(a,'D3',3); in matlab) and there is a 10k ohm resistor in series after the wire from pin 3. the analog read wire is coming from A4 and is reading the voltage directly after the resistor. however, it is reading 3.2 V most of the time. this does not seem possible unless the analog pin is somehow supplying voltage. but it cant be since it is just supposed to be analog reading. very confused.

analogWrite() doesn't produce an analog voltage. It produces a PWM digital output which can be averaged to look like an analog voltage.

You can put an RC (resistor-capacitor) filter on the PWM output, which can then be measured by the analogRead(). Since you have Matlab, you have the perfect tool to design the filter.

okay cool, but im still not sure how I would go about designing the filter in matlab

okay cool, but im still not sure how I would go about designing the filter in matlab

You don't. You design the filter at Radio Shack. it is EXTERNAL HARDWARE.

alright PaulS but from my understanding this would create a DC out. the whole reason I am using a PWM in the first place instead of just digital write is so there is an alternating on/off voltage. So i was thinking i would average the readings I am getting from the readVoltage in matlab, which is either 3.2V or .2V, but this didnt make sense to me as to how it was reading 3.2V, since only 3V are being applied in the first place.

How is matlab reading these values ?

Matlab is reading the voltage through the arduino hardware support package, so using those commands in a while loop and recording them to an array outside of the loop, then a plot function vs time inside the while loop so it plots realtime data

So, you are generating PWM on one pin, and then connecting that to another pin, and trying to read the analog value on that pin ?

I don't know what "writePWMVoltage( )" function call in Matlab will cause the arduino output to do.

If it causes an arduino "analogWrite( )" to occur, the frequency of that is quite low, the time that the output signal is either high, or low, is fairly large compared to the sampling time of the arduino ADC. Therefore, any particular arduino adc sample should be either high, or low, for most samples.

On the other hand, if that matlab function causes the arduino to generate some kind of higher frequency pwm, than the adc is more like to "perceive" some kind of intermediate voltage.

Also, do you have a 5 volt or 3.3 volt arduino ?

Hi, What model arduino are you using?

Tom.... :)

I am using the arduino uno, and the pwm function does not exactly make the digital write function to occur (i wish it did) so it is writing an inputed 3V as a PWM. I decided to just run it ten times as fast, make another array for the voltage, and average it over the last 10 readings. Seems to work reasonably well.

The standard solution for this is an RC (a resistor between the output pin and one side of a capacitor, other side of cap to ground), and you measure voltage on the cap. There are abundant guidelines for selecting appropriate resistor and capacitor values for turning PWM into an analog voltage - in addition to tutorials online, every manufacturer seems to have app notes on that, even manufacturers who don't make anything that outputs PWM, because people try to drive the analog inputs on their parts from PWM outputs.

okay, so i am trying to find the actual problem now instead of covering it up. When i unplug the analog pins from everything, they should be reading 0V, but are not. they are reading a value of like 2 or 3 V. this makes no sense, and I think it is the reason for my errors. any ideas?

The pins are very high impedance and will float if left open. Put a 10K or 100K to ground and all will be well.

okay, but like I said before, Even when I put a resistor(10k) in front of the wire to be reading the voltage, it is still reading 3V, which is what the PWM is supposed to be outputting as a whole. So its like there is no resistor there at all. I doesnt make sense why the analog pin would be reading this. I have tried all the analog pins.

How do you mean "in front of"? Draw a schematic. Hand-drawn is OK.

If it's unplugged and one end of the 10K resistor is floating in the air, you are measuring the voltage of the air. This can actually be a lot stranger than you think. Air is mostly an insulator so it doesn't have any voltage at all and the analogRead() will return random numbers.

by in front of i mean in series with. as shown in the drawing pin 3 should be outputting a 3V PWM voltage, and pin A4 should be reading an analog voltage. because of the resistor being there it should not read 3V, but it does.

The DC input impedance of the analog input is 100 Mohm. This is 10,000 times bigger than your 10K resistor. So your resistor does reduce the voltage at the analog input but only by a very small amount.

Well if you add a capacitor to that, you will have an RC filter and your original problem will be solved. I could tell you the capacitor to use but I think the purpose of the exercise is for you to find the equation that will allow you to calculate it for yourself.