Arduino Duty Cycle

Thread Starter


Joined Jun 29, 2019
hello friends, I want just to simulate a duty cycle under isis proteus using a potentiometer.
that is my schema and Arduino code.

the simulation of a duty cycle doesn't correct as you can see in the picture for d=50%

ton> toff



Joined Apr 11, 2010
Pin 0 on an Arduino is a digital pin (ie NOT an analog pin). It is often not used in a design because it’s reserved for loading a sketch, debugging or general serial communications.

You have set your analog pin to be pin 0.

What you have shown is your pot connected to pin 21 (or thereabouts). I am sure you are confused by the label A0...

A0 is a reserved variable in the Arduino world. You can address pins by their physical number or their reserved names. In your sketch, you are conflating the two names.

Instead of the line:
int analogpin = 0;​
use the line:
#define analogpin A0​
This will cause your sketch to access the correct analog pin.


Joined Apr 11, 2010
But first, believe me when I comment that a) you are not connecting your pot to pin 0, b) you are connecting it to the pin pointed to by the system variable A0 and c) you need to use the map() function.

Created a new thread, posts Moved.E
Last edited by a moderator:


Joined Dec 20, 2010
According to your original code:
Assuming that the pot is at center. Vin is 2.5V, and 1024 = 5V then val = 2.5V = 512
255-(512/3) = 255-170.7 = 84.3 <-- that's nowhere 50%

Should be:
512/1024 * 255 = 127 = 50$
pwm = val/1024 * 255

I dont know why that map function is not working for you
Last edited:


Joined Dec 20, 2010
Try this:
int val = analogRead(A0)
pwm = (val +1) /4 ;;--- same as val/1024 * 255

if that doesnt work the try changing the pin to A1 and the pot to A1
#define analogPin A1
int val analogRead(analogPin)
Last edited: