Introduction: Arduino and PWM

I this tutorial I would like to show you how to use PWM to control the brightness of an LED and speed of a motor. I will explain what is a PWM through these two examples.

Supplies

  1. Arduino Uno
  2. LED (3-5 V of any collor)
  3. 150 Ohm Resistor
  4. L298N Motor Driver
  5. TT Brushed Gear Motor 6 V
  6. 6 V - 12 V Battery Pack

Step 1: Wire the LED

I have already covered an LED wiring tutorial, you can always check it to see the detailes about the wiring.

Here I will only mention that the LEDs long leg (anode) should be connected with the red wire on the image and that the resistor used is 150 Ohm. Just follow the images and connect it.

The LED needs to be connected to the PWM enabled pin which is depicted with a ~ simbol. Here I have used D11.

Step 2: Control the Brightness

To control components with a PWM we use an analogWrite command. You can download the code below and upload the sketch on your arduino board.

The code is beginner friendly and every command line is explained. We will simply go from 0 or off state to the 255 or on state and back again, using the "for" command.

Step 3: Wire the L298N

Follow the images to wire the L298N with the Arduino. You can connect the motor either way, brushed motors don't care about polarity, connecting the motor wires opposite will make the motor simply run in opposite direction.

It's very important to always connect all of the electronics with the common ground, here you can see that on the third image where Arduino and the batteries are connected to the same ground on the L298N.

ENA pin is use to control the speed with the pwm and it needs to go to the digital pin on the arduino able to provide PWM. These pins are marked with ~ simbol. Here we are going to use ~D3 for ENA, D2 for IN1, and D4 for IN2.

IN1 and IN2 are used to change the rotational direction of a motor. More on that with the sketch.

Step 4: Control the Speed

Here you can see a very simple sketch used to control the gear motor with a L298N. You can also download the code below.

This sketch is used to test the capabilities of the gear motor and L298N combination, to see how slow can the motor run. You can use the sketch for this purpose before deciding on the motor and the motor driver for you next project.

Step 5: More!

If you like the way this tutorial describes, take a look at my Youtube Channel. This video shows a lot more, and it's perfect for beginners. The video is only 12 minutes but it will take you much more to complete and understand everything shown there. Take it slow, experiment and learn everyday.