Hari ini saya akan menjelaskan mengenai bagaimana cara membuat sinyal PWM dengan frequency yang diinginkan, pada contoh ini dijelaskan cara membuat sinyal PWM dengan frequency 25 KHz, jika ingin merubah frequency dan duty cycle-nya maka bisa atur pada programnya saja. alat ini difungsikan untuk buck-boost controller, sehingga harus sesuai dengan frequensi dan duty cycle yang diinginkan. sebagai tambahan bahwa alat ini menggunakan potensiometer untuk mengatur duty cyclenya. untuk lebih jelasnya berikut skema dan programnya.
a. Arduino Uno
b. Potensiometer
c. Program Arduino IDE
#include <PWM.h>
//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 125; // duty cycle
int32_t frequency = 25000; //frequency (in Hz)
void setup()
{
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
Serial.begin(9600);
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(led, frequency);
//if the pin frequency was set successfully, turn pin 13 on
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
int sensorValue = analogRead(A0);
if (sensorValue > 255)
{
sensorValue = 255;
}
//float teg = sensorValue * (4.7 / 1023.0);
//use this functions instead of analogWrite on 'initialized' pins
pwmWrite(led, brightness);
//merubah duty cycle sesuai nilai ADC
brightness = sensorValue;
}
//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 125; // duty cycle
int32_t frequency = 25000; //frequency (in Hz)
void setup()
{
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
Serial.begin(9600);
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(led, frequency);
//if the pin frequency was set successfully, turn pin 13 on
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop()
{
int sensorValue = analogRead(A0);
if (sensorValue > 255)
{
sensorValue = 255;
}
//float teg = sensorValue * (4.7 / 1023.0);
//use this functions instead of analogWrite on 'initialized' pins
pwmWrite(led, brightness);
//merubah duty cycle sesuai nilai ADC
brightness = sensorValue;
}
d. VIDEO HASILNYA
salam, mas,
ReplyDeletemw tanya,
maksudnya ini apa ya
/use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9; // the pin that the LED is attached to
kok di keterangannya menggunakan pin 11, tp di program menggunkan pin 9
juga, di inisialisasi outputkan ada di pin 13
pinMode(13, OUTPUT);
makasih