How to use Arduino PWM Pins ???
- You can download the complete simulation along with its Arduino code for Arduino PWM by clicking the below button:
- First of alll, we should know which pins of Arduino can be used for PWM purposes.
- So, if you have a look at the below figure, its an Arduino UNO and all the pins of Arduino UNO which has this sign "~" in front of them are PWM pins.
- If you have a look at the above Arduino UNO image then you can see that "~" this sign is placed in front of six pins.
- So, Arduino UNO PWM Pins are:
- Pin # 3
- Pin # 5
- Pin # 6
- Pin # 9
- Pin # 10
- Pin # 11
- Using these PWM Pins, you can create the PWM pulse which we are gonna do rite now. :)
- So, design a simulation in Proteus as shown in the below figure:
- As you can see in the above figure that I have used LDR Sensor with Arduino UNO and I have plotted the PWM output coming from Arduino UNO on the oscilloscope.
- For PWM output the command used in Arduino is:
analogWrite(PWM_Pin, PWM_Value);
- As, you can see its just an analog Write command and using it you can write any value to the PWM Pin ranging from 0 to 255.
- At 0 the duty cycle of PWM will be 0% and at 255 it will be 100%.
- So, what I did in the above example is I just take the analog value coming from LDR and then transferred it to PWM Pin of Arduino UNO.
- So, now upload the below code in your Arduino board:
int PWMControl= 6; int PWM_Input = A0; int PWM_Value = 0; void setup() { pinMode(PWMControl, OUTPUT); pinMode(PWM_Input, INPUT); Serial.begin(9600); } void loop() { PWM_Value = analogRead(PWM_Input); PWM_Value = map(PWM_Value, 0, 1023, 0, 255); analogWrite(PWMControl, PWM_Value); }
- So, now Get your Arduino Hex File and upload it in your Proteus software.
- You will also need to download Arduino Library for Proteus, if you wanna use this Arduino UNO in Proteus.
- Now, if everything goes fine then you will get results as shown in below figure:
- Now you can see in the above figure that I have shown the PWM pulse in the oscilloscope and now when you change the LDR value then this pulse's PWM will also change.
- You can download the complete simulation with Arduino code by clicking the button above.
- If you have any problems or issues in this Arduino PWM tutorial then let me know in comments.