Coding Experiments with Arduino UNO

Reid Watson
5 min readFeb 21, 2021

What is Arduino?

Over the past couple of weeks I’ve been exploring different applications of web development, and I happened across a fascinating DIY product designed for people who want to explore ways in which programming can connect to electrical engineering.

This product is called the Arduino. Arduino is an open-source hardware and software company, project and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices.

Arduino UNO

The Arduino UNO board pictured above is relatively cheap, and comes with a variety of digital and analog pins that can be used to create simple robots, thermostats, motion detectors, etc. With just a little bit of knowledge about circuitry and electrical engineering, the options are truly limitless.

Arduino Thermostat

In this blog, I’ll go through a brief tutorial of one of the projects that comes with the Arduino Starter Kit, an informative and beginner-friendly microcontroller kit that provides step-by-step instructions on how to build and program Arduino circuits.

To get started, you’ll need to find some hardware that comes with the kit. This project requires: 1 Arduino UNO board, 3 LED Diodes, 3 220 Ohm Resistors, 1 temperature sensor, a breadboard, and a few different wires of various length.

If you are completely unfamiliar with any of these components and what they do, that’s okay! A tutorial book that comes with the starter kit explains everything you need to know about each part, and how circuits function. I won’t go into exhaustive detail about this aspect of the project in this blog, and instead focus on the code that makes the magic happen!

Below is a diagram of how the circuit should look after full assembly.

As you can see in this diagram, electricity flows from the 5V Arduino pin through the red wire and connects to the breadboard. This provides power to the black temperature sensor that comes with the kit, labeled TMP, as well as the LEDs. The TMP then takes a reading of the temperature, and communicates that information back to the Arduino via the green wire.

From here, the code that you will eventually write and upload to the board will process that information and subsequently illuminate 1 to 3 of the red LEDs, depending on change in temperature. In this way, the change in temperature that the TMP senses will be expressed in terms of how many LEDs are lit.

The Code

To get started with programming the board, you’ll need to download the Arduino IDE, which is available for Mac, PC, and Linux, free of charge(although a modest donation is encouraged!).

The Arduino IDE uses a standard API that is essentially a variant of C/C++. Once you’ve downloaded the IDE, plug the Arduino board into your laptop or desktop with a USB-to-microUSB cable.

Then start a new Arduino IDE file, which should look like the image below:

Every snippet of code that you upload to the Arduino board, referred to as a “sketch”, comes pre-loaded with two functions: setup and loop. If you’re unfamiliar with C++, the “void” syntax simply means that these functions will not be returning anything. The setup code will initialize the board and run once. Then the loop function will continue to run repeatedly and indefinitely, as long as the board has power.

void setup()

The setup function will be used primarily to define your inputs and outputs on the board, so that your Arduino knows where to look for incoming and outgoing information. For this project, the setup function will look like this:

First, define constants for the data pin that the TMP sensor is connected to, as well as the baseline temperature that we will use for this project, 20 degrees Celsius. The for loop within the setup function iterates over 3 digital data pins, defines these pins as outputs with the built-in pinMode() function, and then uses the digitalWrite() function to tell the LEDs connected to these pins whether or not they should be turned ON or OFF. digitalWrite() takes two parameters, a pin number, and the word HIGH or LOW, which essentially translates to ON or OFF. Now that this setup function has been completed, the Arduino board can manipulate the LEDs you’ve built in your circuit, using code!

void loop()

Next, the loop function is implemented, which tells the Arduino board what to do with the information gathered from the TMP sensor.

The first line of the loop uses the analogRead() function to grab data from the TMP sensor and save it as a variable. The TMP sensor expresses this data in terms of voltage, so the “temperature” variable is created to convert this value to degrees Celsius. Next, a series of if else statements are written to tell the Arduino board what to do when temperature changes.

Essentially, these conditional statements say that if the temperature is less than 2 degrees higher than the baseline, all 3 LEDs will remain OFF. If the temperature increases between 2 to 4 degrees higher than the baseline, one LED will turn ON. If the temperature increases 4 to 6 degrees, two will turn ON, and so on.

Lastly, the circuit and code are now complete, and all you need to do is upload the code and watch the machine work!

Hopefully this tutorial has shown you that with just a little technical know-how, your coding skills can apply to a variety of topics within robotics and electrical engineering, not just web development! Coding is such a versatile and useful tool, and using programming with Arduino is demonstrative of the fact that the things you can accomplish with computers are only limited by your creativity and imagination.

See you next time!

Sources:

--

--