# Pulse Width Modulation (PWM) in evive Description
Learn how to use Pulse Width Modulation (PWM) to control analog components like motors with Arduino IDE and PictoBlox.

## Introduction

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages between full-on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change or modulate, that pulse width. If you repeat this on-off pattern fast enough, the result is as if the signal is a steady voltage between 0 and 5v controlling the speed of the motor. evive generates analog output in PWM form. It has 12 PWM pins for the same; they are numbered 2 – 13. Pin 13 is internally connected to the pin 13 LED.

## How to use PWM Pins in Arduino IDE

### analogWrite()

Generally, Arduino’s PWM frequency is about 500Hz. In Arduino IDE, we use PWM concept through analogWrite() function. We give a value ranging on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

Syntax:

``analogWrite(pin, value)``

Where pin is the pin to write to and value is the duty cycle between 0 (always off) and 255 (always on).

### Example

In this example, we will control the brightness of pin 13 LED with evive’s potentiometer that is internally connected to the analog pin A9. The brightness of an LED depends on the current flowing through it. If we control the voltage supply to the LED, we can control the current flowing through it, and as a result its brightness. Note that the values that analogRead() can take are from 0 to 1023, whereas those that analogWrite() can take are from 0 to 255. Hence, we need to map the values.
``````/*
This code demonstrate the use of analogWrite(). According to the value
of the potentiometer 1 of evive, the brightness of the LED is controlled.
*/

int LEDPin = 13;   // LED connected to digital pin 13
int PotPin = A9;   // Potentiometer connected to analog pin A9
int val;           // variable to store the read value\

void setup() {
// put your setup code here, to run once:
pinMode(LEDPin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
val = val/4;   //this maps the analog values of potentiometer that range from 0-1023 to values 0-255 for giving analog ouput at pwm pins.
analogWrite(LEDPin, val);   // Sets the brightness according to potentiometer state
delay(50);
}`````` ## How to use PWM Pins in PictoBlox

Set PWM pin () output as () block is an evive extension block. This block set PWM output on evive PWM pins. The user can select the output from a range of 0 to 255.  If the PWM output is 128, then half the time output will be High and for the rest, the output will be Low. Available  pins in evive:

• 12 PWM pins available in evive: 2 to 13

You can select the pin using the drop-down menu.

### Example

Controlling Pin 13 LED brightness using Potentiometer 2 in Arduino Mode: ## Conclusion

In conclusion, Pulse Width Modulation (PWM) is an effective way to use digital signals to control analog components such as motors. It is a technique used to get analog results with digital means by using an onoff pattern to simulate voltages between fullon and off. In Arduino IDE, we use the PWM concept through the analogWrite() function, which takes a value ranging from 0 to 255 and sets the duty cycle accordingly. evive also has 12 PWM pins (213) that can be used to control analog components. Additionally, PictoBlox has the Set PWM pin () output as () block to set the PWM output on evive pins.