PWM is 0-5v? Why did I measure 0-2v?

I hooked the arduino nano pin 9 with this oscope http://www.rigolna.com/products/digital-oscilloscopes/ds1000d/ds1052d/ and measured 0 - 2v, 500mV per division so that is 4 divisions I saw on vertical scale using 1x probe. What did I do wrong?

int outputPin = 9; 


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  
  int ledlevel = analogRead(3);
  int pulse = constrain(ledlevel, 0, 1023);
  pulse = map(pulse, 0, 1023, 0, 255); 
  analogWrite(outputPin, pulse);  //pwm
  Serial.println(pulse);
  delay(100);
  
}

I think you need to set the pin as OUTPUT.

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

Steelmesh:
I hooked the arduino nano pin 9 ... measured 0 - 2v, 500mV per division so that is 4 divisions I saw on vertical scale using 1x probe. What did I do wrong?

In your code you are reading an analog pin, mapping its value (it would be faster to just divide by 4), and printing it. What value are you seeing on the serial monitor?

SurferTim:
I think you need to set the pin as OUTPUT.
analogWrite() - Arduino Reference

The analogWrite() reference page clearly says otherwise. analogWrite() sets the pin to OUTPUT before using it.

I see 5Vp-p on my Uno.

What voltage is your nano running from?

What does "analogWrite()" do with the pin after the 'write'?

Doc

Docedison:
What does "analogWrite()" do with the pin after the 'write'?

Doc

Absolutely nothing.

The code as posted by the OP works perfectly. I see 0-5V on the UNO.

For reference, here is the (truncated) analogWrite code from 1.0.1:

void analogWrite(uint8_t pin, int val)
{
        pinMode(pin, OUTPUT);
        if (val == 0)
        {
                digitalWrite(pin, LOW);
        }
        else if (val == 255)
        {
                digitalWrite(pin, HIGH);
        }
        else
        {
                switch(digitalPinToTimer(pin))
                {
                        ////// lots of cases here for different timers and different chips, eg: (I'm not putting them all in - pointless)
                        // XXX fix needed for atmega8
                        #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
                        case TIMER0A:
                                // connect pwm to pin on timer 0
                                sbi(TCCR0, COM00);
                                OCR0 = val; // set pwm duty
                                break;
                        #endif

                        case NOT_ON_TIMER:
                            default:
                                if (val < 128) {
                                        digitalWrite(pin, LOW);
                                } else {
                                        digitalWrite(pin, HIGH);
                                }
                }
        }
}

Opps, I will check it again with the new code

What new code? The code I posted is what the Arduino does when you call analogWrite.

There is nothing wrong with your code. Your code works fine.

Is your oscilloscope showing the actual pulses ?

Did you have something connected to Pin 9?

Can you attach a screenshot from the scope? When creating a post click on "additional options" below the editor box. You can attach files there.

Yes, a 3xx ohm resistor and LED, but I am sure I hooked the probe between that stuff and pin 9 to get voltage out of arduino.

I will repeat the experiment, and take some pictures of the scope. I am accessing the scope at techshop (freeee), I didn't ever get a scope from craigslist :stuck_out_tongue:

Right now I am prototyping an IR2110 circuit for motor control:

Steelmesh:
Yes, a 3xx ohm resistor and LED, but I am sure I hooked the probe between that stuff and pin 9 to get voltage out of arduino.

If you try to draw more than 40mA out of an I/O pin, its output voltage will drop. I would verify the resistor is actually 300ish ohms and not 30ish ohms.

Also, if you put the probe on the scope's calibration output (next to Ext Trig), you should see a waveform that is around 1.25V in amplitude. I have the DS1052E, attached is a shot of the calibration output for reference. Storage -> Waveform to Bit Map -> External (USB Stick) -> New File -> Save.

NewFile6.bmp (146 KB)