Table of Contents

set PWM pin () output as ()

Description

This block sets the output of a selected PWM pin of an Arduino Uno, Arduino Mega, or Arduino Nano board to a value from 0 to 255. When set to 128, the output will be high for half the time, and low for the other half. This allows users to control the voltage output to an attached device.

Available pins

  1. Arduino Uno: 6 PWM pins available to the user: digital pin 3, 5, 6, 9, 10 and 11
  2. Arduino Mega: 15 PWM pins available to the user: digital pin 2-13, 44-46
  3. Arduino Nano: 6 PWM pins available to the user: digital pin 3, 5, 6, 9, 10 and 11

Example

Discover the power of Pulse Width Modulation (PWM) in Arduino, enabling voltage control and pulse width adjustment.

Introduction

PWM and It’s Applications

Pulse Width Modulation (PWM) is a powerful signal that allows precise control over voltage values and the time for each voltage level. By selectively choosing voltage values and durations, PWM signals can be fine-tuned to meet specific requirements. In PWM signals, the time lengths for the LOW and HIGH states of the pulse can vary, as depicted in the figure below. PWM has various applications and is commonly used to control LEDs and DC motors.

Applications
  1. LED Control: PWM controls the frequency of light emitted by LEDs, making them appear ON and OFF at a frequency detectable by our eyes.
  2. DC Motor Control: In DC motors, PWM acts as a pulse train, delivering high or low electrical power based on the width of the PWM pulses.
PWM pins in Arduino 

Arduino boards have 14 digital input/output pins, six of which can be utilized as PWM outputs (indicated with a dash on the board). The Digital-to-Analog Converter (DAC) channel of Arduino is an 8-bit register, enabling voltage values ranging from 0 to 255 (corresponding to 0V and 5V, respectively).


Understanding Analog Output

Analog signals have variable magnitudes throughout their cycles. Examples of analog signals include the output of smoke sensors and soil moisture sensors. In Arduino, PWM pins can be used to generate analog signals as output.

Circuit Diagram

 

  • LED+ to pin 3
  • LED – to GND

Code

  1. Select Arduino Uno from the boards and connect it via USB.
  2. Create a variable “brightness” and set its initial value to 0.
  3. Use the “repeat until ()” block from the Control palette.

  4. Check the voltage range for PWM pins (0-255) using a comparison operator.
  5. Increase the value of “brightness” by +5 inside the “repeat until ()” block.

  6. Use the “set PWM pin() output as()” block from the Arduino palette to set pin 3 as a PWM output and adjust its voltage level using the “brightness” variable.
  7. Place this block inside the “repeat until ()” block.
  8. Set the PWM value to brightness and  change the brightness by +5.
  9. Again add a repeat until () block and check if the brightness is greater the 0
  10. Now change the brightness by -5 and add give this value to PWM pin 3
  11. Merge all the code blocks and insert them inside the “forever” block. Also, add the “when flag clicked” event.
Script:

Output: GIF Arduino
Read More
All articles loaded
No more articles to load