The best tools to make your project dreams come true

Login or Signup
USD

4/10/2018 | By Maker.io Staff

How to Add a Simple Circuit to Your Arduino

The Arduino is a fantastic development board for testing ideas and products, but unless you learn how to connect it to external circuits, it is rather pointless! In this article, we will learn how to do just that!

External Devices and Circuits

Some of the strongest features of the Arduino are its GPIO pins, which allow it to send electrical signals to the outside world and read them! But using GPIO is a two-part problem; you must deal with it correctly in both hardware and software! So, to start, let’s look at the hardware side of external connections to the Arduino.

Most Arduinos have pin headers on the outer perimeter, which are used to connect to circuits. These pins can have many different functions, including I2C, SPI, and UART, but they are usually used in one of two modes: digital input and digital output. When configured as a digital input, the pin can read digital values from a wire, and these values can either be 1 or 0, which correspond to VCC and 0V, respectively. When configured as a digital output, the pin can set the digital value on a wire. So, if the pin is instructed to write a digital 1, the Arduino will set the voltage on the wire equal to VCC (typically 5V), and if the pin is instructed to write a digital 0, then the voltage will be set to 0V. In this how-to, we will learn how to turn an LED on and off, as well as how to read the state of an external button (all connected on an external breadboard).

How to Add a Simple Circuit to Your Arduino

Before you connect a circuit to the Arduino, you need to determine if protection circuitry is needed. If the proper protection and/or driving circuitry is not used, you run the risk of permanently damaging the Arduino. So, let’s see how to protect our Arduinos properly!

Input

If you are using a GPIO pin as a digital input pin, you need to find out the maximum and minimum voltage that the pin can handle. The easiest way to determine this is to either consult the Arduino forum or find the datasheet for the main controller and read its I/O pin tolerances. For example, the Arduino Uno is based on the Atmega328, and, if we consult the data sheet, page 365 has a table of electrical characteristics. The third row mentions that the maximum and minimum input voltage for any pin (except RESET) is -0.5V to VCC+0.5V. This means if our Arduino uses a 5V source, then the maximum input voltage is 5.5V and the lowest is -0.5V.

How to Add a Simple Circuit to Your Arduino

If you accept that your input may go beyond these ratings, you can use clamp diodes to clip the voltage when it goes beyond these values. Below is a simple circuit example of a single resistor and Zener diode that can be used to protect inputs from accidental damage and ESD. The resistor is used to limit current flowing to the Arduino, should something horrible happen! This circuit is not needed in many projects, but, for those who want to add just that little extra bit of protection, it is not a bad idea.

How to Add a Simple Circuit to Your Arduino

Output

Connecting an Arduino pin to an output device also requires a lot of care, and the first rating you should check is the expected current draw. The Arduino Uno pins are rated to a maximum output current of 40mA, but this does not mean that, if an external circuit attempts to draw more, the Arduino will refuse. Instead, the Arduino will happily give out as much current as it can, but this will very quickly result in the device overheating and killing itself.

This is why you need to find out how much current your external circuit wants! If it is equal or less than what the Arduino is rated to give out, you should have no problems connecting the Arduino directly to the circuit. If, however, the circuit needs more, you will need to use some kind of transistor-driving circuit. The circuit below shows a relay, which is controlled using a transistor that is controlled by an Arduino output pin.

How to Add a Simple Circuit to Your Arduino

You will also need to consider is the circuit itself! The number of times people have connected LEDs to an Arduino output without a series resistor is frightening, and it can damage both the LED and the Arduino! So, make sure that any current-drawing device that is directly connected to an Arduino output pin has some kind of series-current-limiting resistor!

How to Add a Simple Circuit to Your Arduino

Basic Arduino Circuit Example

Now that we know what to look out for when connecting external devices to the Arduino, we can go ahead and create our circuit. The circuit uses an LED with a series resistor connected to pin 2, while a tactile switch is connected to pin 3. A pull-up resistor is also used on pin 3 so that, during normal operation, pin 3 is connected to VCC through the resistor. But, when the button is pressed, the voltage seen by the pin will be 0V. This means when the button is not pressed, the pin sees a digital 1, and when the button is pressed the pin sees 0.

How to Add a Simple Circuit to Your Arduino

The Software Side

With the hardware built, it’s time to create an Arduino sketch that will make the LED flash when the button is pressed. First, start the IDE and then create a new project/sketch, which we will call “Arduino Button Flasher”. With the sketch made, it’s time to put in some code.

Now, using IO pins on the Arduino is incredibly simple, and it only requires a few functions:

pinMode(pin, in/out)

digitalWrite(pin, value)

digitalRead(pin)

The first function you will need to use is pinMode, which is used to declare whether a pin is an INPUT or an OUTPUT. The code example below shows how pins can be configured as such.

How to Add a Simple Circuit to Your Arduino

The second function you will need to use is digitalWrite, and this is used to write digital values to a pin.

How to Add a Simple Circuit to Your Arduino

The third function you will need to use is digitalRead; this is used to read the digital value present on a pin.

How to Add a Simple Circuit to Your Arduino

Code Example

Now it’s time to upload the code below to the Arduino! The external switch should make the external LED flash five times!

How to Add a Simple Circuit to Your Arduino