Arduino Pin By Pin

for Chemists

M Emre Sener

2017-10-01

Arduino

Arduino1 Arduino website, (Genuino in the U.S)2 Arduino brand is called Genuino in the U.S. due to some copyright issues that the designing group had. Nobody ‘really’ calls them Genuino is a family of prototyping platforms. They come in all sorts of shapes sizes and capabilities. Portrait of a happy family

The most popular member is the UNO that you are probably looing at right now. If you haven’t done it yet, unbox the UNO and jump in. We will try to go through the things UNO is capable of, pin by pin!

USB B Port

Possibly the most straightforward3 For the afficionados: The microcontroller (MCU) on the UNO actually has no built-in way for talking to a computer over USB (unless you have an ancient computer with a serial port). This is done with a serial to USB chip that sits between the MCU and the USB port. port on the Arduino. USB allows your computer to power up the UNO and also transfer data.

The UNO (normally) runs with 5V DC4 The power draw is around 45 mA, quite small but amounts to about a day on a 9V battery, interesting read on getting an Arduino to run on 100 nano amperes (a year on a coin cell battery), precisely what the USB port on your computer provides. From now on, whenever ‘high level’ or ‘maximum voltage’ is mentioned, it’ll be referring to the 5V DC that the UNO runs on. Words of caution: UNO a low voltage device, it’s unlikely that you’ll zap yourself. But if the bottom of the Arduino comes into contact with conductive surfaces and causes a short circuit, either your computer, the UNO or possibly both might die an early death. Fingers are ok, stray pieces of wire and metal desk tops, not ok.

Set up your UNO via USB

Follow this guide here and stick to the software integrated development environment (IDE) and not the web-based one. Following the guide will help you get used to the basics. In general, don’t be afraid to play around, mess with code, get errors (and solve them) and upload pieces of code that the tutorials mention. There is no easy way of destroying the UNO using just your computer (unless you’re using it as a high tech hammer).

Power Jack

Carrying a laptop everywhere the UNO goes just to keep it running defeats the purpose of a MICRO-controller (emphasis added). That’s why the UNO comes with a barrel jack to power it via a 9V battery5 If you recall the last section, the UNO actually runs on 5V, using a 9V battery means that the UNO ‘regulates’ the 9V down to the 5V it can cope with, the 4 volts ‘dropped’ on the way is lost as heat, a highly inefficient way to power such a low power device. There are better ways of powering the UNO or a wall adapter.

5V and 3V3

These two pins on the UNO are meant for providing power at their advertised voltages. The 5V pin is connected directly to USB power, limited at around 400 mA whereas the 3.3V is sourced from an on-board regulator capable of sourcing 150 mA. Any high power project should ideally use a dedicated power source and not the 5v and 3.3v pins.6 How do you know whether you can use the UNO to source current? For resistive loads (heater, lamp etc.) measure the resistance and use Ohm’s law \(V=IxR\) otherwise check datasheets. As a rule of thumb, if the UNO is warm to the touch, you’re sourcing too much current.

Ground (GND)

The ground pins on the UNO allow it to ‘sink’ current. Ground is also the current return path for any digital signals that UNO uses to communicate with sensors, and other devices. As a rule of thumb if UNO is connected to another piece of electronics, they also have to have their GNDs connected, otherwise things won’t work and you’ll be left scratching your head.

A second (highly related) use of GND is as the 0V reference. Any potential you might read will have GND as it’s 0V reference this applies to analog AND digital signals (1’s, 0’s and 0.5’s). To demonstrate the significance of GND, look at the plots below for four different scenarios. Try it for yourself, code is available here, copy and paste it and after uploading to the Arduino connect the A0 pin to 5V, 3.3V and GND pins and look at the values you get7 You might be wondering why you’re reading numbers like 1023 and 669 instead of 5 and 3.3, we’ll get to this in the ADC section, if you’re curious, give converting your 5.5V result, 1023, into binary. To plot them go to Tools -> Serial Plotter in the Arduino IDE. For something more interesting, leave the A0 pin unconnected8 Floating inputs do as their name implies, just because it is connected to ‘nothing’ does not mean that the pin is at 0 volts, it is instead floating in between ‘high’ and ‘low’. Touch the A0 pin while the plotter is on, did you see any response?.. and see what it does.

Four plots of Analog Readouts, open in new page/tab for full-size

An Aside, the Microcontroller

All the “interesting” parts of programming and using the UNO depend on the microcomtroller (MCU). UNO is in essence a bunch of electonic components that are all designed to make the MCU work. A microcontroller is a large integrated circuit that is capable of basic maths, time-keeping and controlling input and outputs (producing a 5 or 0 V output) depending on its programming, much like the CPU in your computer with some embedded memory9 Check the datasheet of ATMEGA328P to see what the UNO is capable of. This +20 year old chip boasts 20MHz top speed and 2 KB of RAM (about a million times less memory and a thousand times slower clock speed compared to the average personal computer).

It might sound complicated, but programming the little processor is quite easy. You have already done it multiple times if you’ve been following the tutorial. The long chip on the UNO is the microcontroller, everything else on the board is ‘life-support’ for this central IC. Do get a better understanding of the UNO’s capabilities, look at the datasheets online and try to answer these questions;

Can you think of any problems these limitations can cause?

Analog Input Pins / ADC

Analog pins are used to read voltages from external sources such as sensors or external circuitry, you have already read the output of the ADC in Ground. The pins marked with A are the Analog pins, they are all directly connected to the microcontrollers ADC. Analog inputs are the bread and butter of any sensing application. You can think of the microcontroller as a bridge between analog signals10 For our purposes, any time we are trying to measure a voltage in the wild, we consider it to be analog. Digital signals on the other hand are tame and discerete, either at 5V or 0V..

Make sure that the code you used in Ground is uploaded. Set up the circuit below. Turn on Serial Plotter, and turn the knob on the potentiometer, what do you see?11 While you’re at it, place an LED between the GND and A0 (we’ll be using the LED as a light detector not as a light emitter), what number do you read? Shine light on it with your phone or a flashlight, did the value change?

Digital Pins

The digital pins are also called GPIO (General Purpose Input / Output) pins, they can either be used as input (surprise!) to see if the signal is digital high (“1” or 5V) or low (“0” or 0 V). Or output, to bring the pin level to 0 volts. These pins can be used for communication between integrated circuits and the UNO, to see if buttons are pressed or to turn things on and off using transistors.

Do not be fooled that the pins provide 5V, each digital pin sink or source up to 40 mA and not much more. This is enough to power an LED Do not use these to power external devices. A good way to turn things on and off using the digital pins is to use a power transistor. Most of the pins on the board are digital pins To see what the output can do, wire the UNO up with a led and a 100-200 ohm resistor as below, and upload this piece of code. You’ll see the LED blinking, this is the “Hello World!” equivalent of microcontrollers. This might be a good time to look at the code and see what it does. Read the comments, change the numbers and see what happens.

Now you’re ready to use the UNO and its digital pins to read an external sensor. We’ll be using the low cost DHT11 temperature and humidity sensor which communicates with the UNO through the digital pins. Follow the tutorial here to get the temperature and humidity readings in the serial monitor. Congratulations, you’ve managed to run your first sensor with the UNO.

Analog Output (PWM)

So far we’ve seen analog output at digital intput/output. What if we wanted to output some voltage?

The pins marked with a wavy line are the PWM pins. Perhaps even vary it with time to get different shaped signals, or to make our blinking LED fade in and out or set it at a given brightness? What we need is analog output, sadly the UNO is not capable of giving us arbitrary voltages. We have to settle for the next best thing, pulse width modulation (PWM)^12 If you’ve ever heard a charger, a power supply or just about any modern piece of electronics hum, changing pitch seemingly at random, there is a chance that you’ve heard PWM. The usual 50Hz transformer hum is slowly being phased out

While using PWM the UNO turns a 5V source on and off very quickly^13 For the connoisseurs: The PWM operates at a fixed frequency, and some pins are faster than others, depending on the timer that drives the pin. With some low-level manipulation the pin timings can be changed to make the PWM frequency faster or slower., the duration of the ON time vs the off time allows us to have an average voltage between 0 and 5V. To see what the waveform looks like, connect the UNO as shown below, run the code here and turn on Serial plotter to see the conga line of PWM signals.

Reading the PWM signal

Reading the PWM signal

Can you see any problems with using PWM instead of a true voltage source? Look up how to “smooth” out PWM signals from the UNO.

Now place an LED between the pin 3 and ground of UNO, watching out for the polarity. Run the code here. Can you see the LED fading in and out? This relies on your persistence of vision rather than a continuous change in voltage. Try playing around with the code, watch what the LED does.