Programming Arduino book sketch 6-08

I am working the exercises in this book and am having some trouble with sketch 6-08 having to do with Analog Outputs. No matter what number I type in the Serial Monitor 0-5 I get the same result displaying on my multi meter. The number I get is 2.15. If I type 3 it reads 2.15 and if I type 5 it reads 2.15. I have the multi meter set on the 20 mark in the V section of the meter just like in the picture on page 101 Figure 6-11. The ground lead (black cable) is connected to the wire from one of the GND pins and the red cable is connected to the wire from pin 3 on the Arduino. I am using the 1.0 version if the IDE.

Has anyone else been working from this book and had a similar problem?

All the other example I have tried out have worked as expected.

Can you post the sketch and anything connected?
Is it a digital output, and you haven't used pinMode (pinX, OUTPUT); ?

Here is the sketch from the book that I'm using.

//sketch 6-08
int outputPin = 3;

void setup()
{
pinMode(outputPin, OUTPUT);
Serial.begin(9600);
Serial.println("Enter Volts 0 to 5");
}

void loop()
{
if(Serial.available() > 0)
{
char ch = Serial.read();
int volts = (ch - '0') * 51;
analogWrite(outputPin, volts);
}
}

I have a wire plugged into pin3 and another wire plugged into a ground pin. The red wire from the meter is connected to the pin 3 wire and the black wire from the meter is connected to the ground wire. When I upload the code to the Arduino and open the Serial Monitor and type a number from 1 to 5 and hit enter the readout on the meter says 2.15 and some times 2.16. According to the book If I type 3 and hit enter the read out on the meter should be 3 volts. I think if I were type 5 the meter should display 5 volts. The dial on the multi meter is set at V 20.

I don't know what else to say about it.

Do the other PWM pins work? Maybe 3 is damaged.

If you write a simple on, off sketch, say at a 1 second rate, does the pin work?

Just a thought, but is your meter set to DC?

Does it say 5V when you connect it to the 5V socket on the Arduino?

Is the negative lead from the meter connected to GND?

Is the meter probe lead in the right socket for Volts on the multimeter? Usually there are separate sockets for current measurement.

Hope you are enjoying my book :slight_smile:

Simon Monk.

Hi

I'm having the exact same problem, the meter is wired up and set correctly. I tried different pwm pins and the same thing happened, then I checked the serial monitor to see the value of volts and what happened was that it would display the right value eg. 255 when 5 is pressed but then a new value,-1938 gets printed immediately no matter what I type -1938 comes afterwards immediately. I'm stumped and can't think of any way to fix this...