Understanding Digital and Analog Pins in Arduino - Lesson #5
by lucascreator in Circuits > Arduino
13 Views, 1 Favorites, 0 Comments
Understanding Digital and Analog Pins in Arduino - Lesson #5

Learn about analog and digital pins and master PWM with Arduino. This is the foundation of reading sensors and controlling components.
I've recently posted a video on YouTube explaining everything you can read in this article.
Supplies






1 x DFRobot MindPlus Arduino Coding Kit
1 x DFRobot Arduino Uno
1 x DFRobot I/O Expansion Shield
1 x DFRobot Digital RGB LED Module
1 x DFRobot 3-pin cable
1 x DFRobot Digital Sensor Cable (30cm)
Introduction
Have you ever wondered how your Arduino can sense the world or control lights, motors, or robots? The answer lies in something deceptively simple: the pins.
In this tutorial, we’ll explore the fundamentals of digital and analog pins, learn how to simulate analog output using PWM (Pulse Width Modulation), and finally apply what we learned to a real RGB LED fading project using a single digital pin. If you're just starting your Arduino journey, this guide is tailored for you.
This is lesson 5 of our 24-part series: Arduino for Beginners. If you're just joining us, we recommend reviewing lesson 1-4 to get up to speed.
Sponsor




This tutorial is proudly sponsored by DFRobot, a global leader in open-source hardware.
All components used in this project - including the Arduino Uno, RGB module, and expansion shield - come from the MindPlus Arduino Coding Kit, an all-in-one solution designed to help beginners get hands-on with electronics without worrying about compatibility or missing parts.
In addition to educational kits, DFRobot offers a wide range of components: sensors, microcontrollers, robotics kits, and even powerful single-board computers like the LattePanda Sigma.
If you're looking for reliable, well-documented components at an affordable price, check out DFRobot’s store.
Understanding Arduino Pins


When you look at your Arduino board, you’ll notice two key labels: digital and analog in. These define the type of signal each pin can handle and play a crucial role in every Arduino project.
Digital Pins
Digital pins operate at two distinct voltage levels: 0V (LOW) or 5V (HIGH). This binary behavior makes them ideal for applications that require simple ON/OFF control, such as turning LEDs on or off, reading the state of a push button, or activating relays. On the Arduino Uno, digital pins are numbered 0 to 13.
By default, digital pins are configured as inputs. However, they can also be set as outputs using the pinMode() function in your code. This flexibility allows digital pins to either receive digital signals (from sensors or switches) or send digital signals (to actuators or other components).
Analog Pins
Analog pins on the Arduino are designed to read varying voltage levels, typically in the range of 0 to 5 volts. These continuous signals differ from the ON/OFF nature of digital inputs. Devices like potentiometers, light sensors, or temperature sensors often output analog voltages.
It's important to note that analog pins cannot output true analog voltages. However, in applications where analog-like output is needed, such as dimming an LED or controlling motor speed, Arduino uses a technique called Pulse Width Modulation (PWM) to simulate analog behavior using digital signals.
Pulse Width Modulation



PWM allows the Arduino to approximate analog output using a digital pin. This is done by switching the pin ON and OFF rapidly, generating a square wave. The key parameter here is the duty cycle, which defines the percentage of time the signal stays HIGH during each cycle.
For example, to create a square wave (PWM) we have:
- analogWrite(255) results in a 100% duty cycle, meaning the signal stays HIGH all the time, equivalent to 5V.
- analogWrite(127) gives a 50% duty cycle, where the signal is HIGH half the time and LOW the other half, perceived as 2.5V on average.
- analogWrite(0) corresponds to no duty cycle, so there won't be current flowing from the pin.
When this switching happens fast enough (typically around 500 Hz on most Arduino boards), connected devices like LEDs or motors respond as if they were receiving a constant analog voltage. For instance, an LED will appear dimmer with a lower duty cycle and brighter with a higher one.
The length of each PWM cycle is determined by the frequency, and the amount of time the signal is HIGH within that cycle is the pulse width. By modulating this width, Arduino can simulate different voltage levels using only digital outputs.
Important Notes on PWM and Pin Compatibility

Not all digital pins support PWM output. On the Arduino Uno, for example, only pins marked with a tilde symbol (~) are PWM-capable. Always refer to your board's pinout diagram to verify which pins can be used for PWM.
Although analog pins are designed for input, many Arduino boards allow you to repurpose analog pins as digital I/O. If your project requires more digital pins than what's available, this can be a helpful workaround - just make sure to consult your board’s technical specifications to confirm compatibility.
We had enough theory for now. Let's move on and get our hands dirty.
Project

Now that you understand digital, analog, and PWM concepts, let's apply them in a real-world project.
We'll control a digital RGB LED using just one PWM-capable pin and simulate smooth fading transitions through all primary and mixed colors.
Circuit Setup





1. Connect the I/O Expansion Shield to the Arduino Uno.
2. Plug the RGB module into digital pin D3 using the 3-pin cable.
3. Wire color referece:
- Green wire → Signal (DI)
- Red wire → 5V (VCC)
- Black wire → GND
Note: this RGB module uses NeoPixel protocol, meaning all three colors (red, green, blue) are controlled via a single data pin,much simpler than traditional RGB modules that require three separate PWM pins.
Code





Arduino IDE
Now that everything is set up, it’s time to start coding.Open the Arduino Cloud Editor, just like we did in the previous lessons. Then, head over to the GitHub repository for this series and copy the code for today’s project.
We used the Adafruit NeoPixel library to control the RGB LED. This tool simplifies communication with digital RGB LEDs using a single signal wire. Additionally, it manages the timing and color data needed to drive compatible LEDs.
Inside the setup function, we have initialize the communication with the RGB module.Now let's take a look at the loop function.
This function is calling another one called fadeInOut - a custom routine definied in the end of the sketch. This function takes three arguments: the values for red, green, and blue.
Each call to fadeInOut() tells the RGB LED to display a specific color by mixing these three primary color components. It first fades the LED in (increasing brightness from 0 to full) and then fades it out (back to 0).
After all the colors are displayed with fade-in and fade-out effects, the loop repeats from the beginning. This creates a continuous and smooth color-cycling animation, unless you unplug your Arduino.
If this code feels a bit confusing right now, don’t worry - you’re not supposed to fully understand it yet.In the next class, we’ll dive deep into coding fundamentals.
We’ll explore loop structures, conditional statements, logic operators, math operations, and more. These concepts are the building blocks of everything we’ll do going forward.Just stick with it - everything will start to click in lesson 6.
Results







Now that you understood the code, let's upload it.
Mind+
.png)
In this lesson, we won't use Mind+ because, unfortunetely, it doesn't support the digital RGB module. But we will certainly come back to this tool in future projects.
Conclusion
In this lesson, you’ve learned:
- The difference between digital and analog pins.
- How to use PWM to simulate analog output.
- How to wire and program a digital RGB LED using NeoPixel.
You now have the foundational knowledge to control brightness, color, and effects with just one wire - an essential skill for displays, indicators, and lighting effects.Thank you for reading this article, and I'll see you in the next one.