At first you had the scope AC (Alternating Current) coupled. Then you switched it to DC (Direct Current) coupled.
AC coupling is used to remove the DC component of a signal. It places a large capacitor between the probe and the internal amplifier. This is used, for instance, if you want to see a small signal with a large DC offset. But it can also attenuate and / or alter signals that are low in frequency.
When a PWM signal is AC coupled to an oscilloscope, an interesting phenomenon occurs. As the duty cycle goes from a minimum (almost always off) to a maximum (almost always on) the DC component of the PWM signal changes. Since the DC component is blocked by the AC coupling, the PWM signal appears to move lower on the screen as the duty cycle increases.
DC coupling is where there is no coupling capacitor between the probe and the internal amplifier. Everything comes through. For digital electronics where the signal travels a small distance from 0 to 5 volts or from 0 to 3.3 volts, this is the best way to use the scope.
analogWrite
call inloop()
- it only needs to be called once (the hardware automatically generates the PWM signal - no software is involved). It can and should be moved tosetup()
.