How to Generate an Analog Output with Arduino UNO

How to Generate an Analog Output with Arduino UNO Featured Image

Following my last tutorial, now I would like to show you how to generate an analog output signal using Arduino UNO. With this tutorial, you will learn how to use an analog output to control the intensity of the LEDs. Being one of the easiest and cheapest ways to regulate the luminous intensity, using a simple circuit like this.

Arduino Uno


A summary of the hardware technical specifications will be presented for Arduino Uno board, a microcontroller that is based on the Microchip ATmega328P and developed by Arduino.cc. It has a set of digital and analog pins, being 14 of these digital pins, 6 of them able to perform PWM (Pulse-width modulation), as well as having 6 analog pins, as you can check in Figure 1.

Figure 1 – Arduino Uno pins (https://lobodarobotica.com/blog/arduino-uno-pinout/)

These boards can be powered via USB cable or by a 7 to 20 volt transformer, you can check more relevant technical specifications below in Table 1.

MicrocontrollerATmega168
Operating Voltage5V
Input Voltage (recommended)7 – 12V
Input Voltage (limits)6 – 20V
Digital I/O Pins14
Analog Input Pins6
DC Current per I/O Pin40 mA
DC Current for 3.3V Pin50 mA
Flash Memory32 KB (ATmega328)
SRAM2 KB (ATmega328)
EEPROM1 KB (ATmega328)
Clock Speed16 MHz
Table 1 – Arduino UNO specifications

Parts Required

  • Arduino Uno;
  • USB cable 2.0 type A / B;
  • Jumper wires;
  • Breadboard;
  • LED;
  • 220 ohm resistors.

Building the Circuit

With what was described above, we can make the most diverse applications. In this case, we will make a small counter of the light intensity of an LED using Arduino Uno for that. To start we need the breadboard, where we will place all the necessary connections to turn on the LED (Figure 2). For this, we will use a jumper to connect pin 11 to the 220 ohm resistor and the Arduino GND pint to the LED anode.

A 220 ohm resistor is used to limit the current, preventing it from burning the LED used (normal consumption of an LED is about 15 mA).

Note: it should be noted that the Arduino UNO (hardware used in this tutorial) only contains 6 pins that are analog outputs, marked with a “~”. These are 8 bits, the value is placed between “0” and “255”, corresponding to a range of 0 to 5 Volts.

Figure 2 – Arduino and LED connections.

Code

Step 1: In this case, we will use the open source IDE (integrated development environment) available by Arduino. If you haven’t installed it yet, you can download the latest version here: https://www.arduino.cc/en/Main/Donate

Step 2: After downloading, you should install it.

Step 3: Open the IDE and select your card in “Tools -> Board -> Board Manager…”.

Step 4: Select your COM port “Tools -> Port->…”.

Step 5: Now copy the code below to your Arduino IDE project and save it.

int leds [] = {13};

void setup()
{

    for(int i=0;i<4;i++)
    pinMode(leds[i],OUTPUT);
}

void loop()
{
    int valor=analogRead(A0);
    int nleds=valor/256;
    int ultimoLed=valor%256;

    for(int i=0;i<4;i++)
        if(nleds>i) analogWrite(leds[i],255);
        else if(nleds==i) analogWrite(leds[i],ultimoLed);
        else digitalWrite(leds[i],0);
}

Step 6: Connect your Arduino to a PC using USB 2.0 Type A / B to USB cable and upload the code by pressing the upload button.

Endnotes

If you need more details about this tutorial just leave a comment below that I will be answering as soon as possible, hope you like the result. If you liked this tutorial, you can visit other works that have already been published on our site https://www.geekering.com/, besides that you can see in particular the posts published by me in https://www.geekering.com/author/jaimesilva/.

Leave a Reply

Your email address will not be published.