Arduino Duty Cycle

Thread Starter

h-Name

Joined Jun 29, 2019
8
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
 

Attachments

djsfantasi

Joined Apr 11, 2010
9,156
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.
 

djsfantasi

Joined Apr 11, 2010
9,156
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.

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

iimagine

Joined Dec 20, 2010
511
According to your original code:
255-val/3
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$
So:
pwm = val/1024 * 255

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

iimagine

Joined Dec 20, 2010
511
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:
Top