Simon Monk pwm sketch won't change output

I bought Simon Monk's book Arduino Programming Next Steps. I'm using the Arduino Uno. I'm using the Arduino IDE 1.8.10 and 1.8.11 ( I was wondering if an updated version would help).

I've done all the sketches up to this one and they've all worked as described. I can do the Blink sketch (without blinking an eye :o ) just to make sure there wasn't something wrong with my Uno (actually tried 2 and got the same thing on both). Here's the sketch that isn't working so you don't have to open:

// sketch 01_07_pwm
int pwmPin = 6;

void setup()
{
  pinMode(pwmPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    int dutyCycle = Serial.parseInt(); 
    analogWrite(pwmPin, dutyCycle);
  }
}

I enter a number (as the book instructs) from 0-255 expecting a change in voltage between #6 and GND but there is none. It is supposed to adjust the voltage from 0-5. It totally seems like it should work but it doesn't (somewhere around .003 mv). Voltage is set for DC on the meter, probes on #6 and Gnd. Nothing shows or updates in the Serial Monitor. When I enter a number, say 49, I click Send, but don't see anything more on the Serial Monitor and there's no change on the voltage. I'm sure I'm doing simply dumb.

As I say, I'm pretty sure its just me.

Good job using the code tags with your post.

I think there are several things to consider.

  1. analogWrite() does not put out a constant analog voltage, but rather a stream of rectangular waves with different high and low times which can appear as a voltage value between 0 and 5v. See https://www.arduino.cc/en/tutorial/PWM

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

It's not clear what you will see with a voltmeter. Use an led with a series resistor between pin 6 and ground to see the effects of this sketch.

2). Make sure that your monitor is set for "no line ending". The .parseInt() function will treat the line endings as 0's.

  1. If you want to see something on the Serial monitor, I suggest adding a Serial print comment after the value is read.
if (Serial.available())
  {
    int dutyCycle = Serial.parseInt();
    Serial.println(dutyCycle);
    analogWrite(pwmPin, dutyCycle);
  }

That's awesome information. Thanks so very much. Concise and helpful. I'll try what you suggest. The LED idea and the settings on the Serial Monitor will help, I'm sure. Thank you CattleDog!