High voltage drop on Nano outputs caused by sketch

Here's the signal I see on pin D2 with your original code.

Frequency is around 16KHz, duty cycle around 70%, peak voltage 4.6V, average voltage 4.2~4.3V

Here's signal from D9. Frequency is around 16KHz, duty cycle around 10%, peak voltage 4.6V, average voltage 1.6~1.7V