Trending

Variable PWM signal using Arduino Serial monitor

Generating a PWM signal with a variable duty cycle using an Arduino with the serial monitor.


Introduction- 

Pulse width modulation(PWM) is a method of reducing the average power delivered to the load by the electrical signal, by effectively chopping it up in parts. It works by switching on and off the supply at a very fast rate. The longer the switch is on as compared to the off time, the greater the power transferred. Existing application of PWM include, but are not limited to - 

1. Fan speed control
2. LED Dimmers
3. Electric motor speed control

Duty cycle- 

The term duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time.
Duty cycle is calculated by the equation - 
DC = Time On / Time period


Arduino Code- 

void setup() {
pinMode(6,OUTPUT);
Serial.begin(9600);
}

void loop() {
  while(Serial.available()>0)
  { int data = Serial.parseInt();
    data = constrain(data,0,255);
    if(Serial.read() == '\n'){
    analogWrite(6,data);
  }
  }
}



The code will allow us to edit the PWM signal output using the serial monitor.
0- 0% duty cycle , 255 - 100% duty cycle.

According to the above-written code, pin 6 of the Arduino will produce the PWM signal with the duty cycle as written in the serial monitor.

Here are some waveforms observed in Arduino based oscilloscope. (Green line)


At 0% Duty cycle - analogWrite(0)

At 25%  Duty cycle - analogWrite(64)

At 50% Duty cycle - analogWrite(127)

At 75% Duty cycle - analogWrite(191)

At 100% Duty cycle - analogWrite(255)

No comments