Measuring voltages with arduino

I want to measure the output of a sensor which on conditioning gives an output of +/- 8.45V. What is a way to measure voltages using analog pins?

I have attached the code. The errors are as follows

  1. Program enters the if condition irrespective of whether the condition is satisfied or not.
  2. Not able to calculate correct input voltages ( even after I used a half wave rectifier to get +8.45 and 0 V)

Finalcode.ino (668 Bytes)

Here's the first mistake:

if (x=1){

The operator you need here is ==, not = You make this mistake a couple of times in your code. So you need:

if (x==1) {

Here's another:

map(sensorvalue, 0,1023,0,8) ;

doesn't really do anything. I think you meant

voltage = map(sensorvalue, 0,1023,0,8) ;

That's the mistake in your code but I'm not sure how well it'll work in practice - the analog inputs can only measure 0V to 5V

STOP

That's some dangerous stuff right here. :wink:
The voltage on any Arduino pin cannot exceed the Vcc voltage. 8.45V on an analog pin will probably just fry the Arduino.

You need a voltage divider to 'convert' it to a voltage between 0V and 5V (or between 0V and 3.3V when using a 3.3V board). (You can't measure negative voltages, so a rectifier is probably a good idea, just remember that diodes have a forward voltage drop, so that will influence your readings.)

Read these:
Arduino Playground: Direct Math Voltmeter
A Beginner's Guide to Arduino: Analog inputs: Measuring voltages

Pieter

I think youll need/want an external op-amp circuit to rescale/shift the -8.45 .. 8.45 to 0..5v.

wg0z:
I think youll need/want an external op-amp circuit to rescale/shift the -8.45 .. 8.45 to 0..5v.

An op-amp is unnecessary, just two resistors will do. You could use an op-amp as a buffer, but you'd lose the upper and lower portions of your range if you don't use a special rail-to-rail op-amp.