Arduino Uno Basics - DigitalReadSerial

Arduino Uno is a really good way to learn about Electronics components. When you use a component with Arduino Uno, you can learn its functioning in many different ways and control it's behavior by programming Arduino.

Let us look at one Example from Arduino. This example can be found in the Arduino IDE at

We will be using a Tactile Switch that comes with the Arduino Kit. I got 5 of them in my Kit. I hope you are already equipped with a Arduino Kit. If not, take a look at this kit on Amazon

These are the Tact/ Tactile switches that I found in my kit. They are pretty small though they appear a lot bigger in this image. if you notice the back side of the switch, there is a groove. It marks the two pins that are connected with each other thus forming two sides of the switch. The switch is a little bit longer along the length of the groove. The switch will fit on a breadboard over the middle groove if you place its longer side across it. It won't fit on the shorter side. 

A Tact switch is an ON/OFF electronics switch that is only on if the button is pressed. It will be in OFF state if you release your finger over the switch button. Tact switches are tactile electromechanical switches used for keyboards and keypads etc.

Be careful while pressing the switch on a breadboard. Use the front pin of a connecting wire to loosen up the breadboard clip by inserting a pin into the breadboard holes before placing a Tact switch. Do not press too hard. You may risk bending its pins if you are placing the switch from the shorter side. You can see in the first image how the Switch is placed over the middle groove of the breadboard. All the breadboards big or small have a similar groove in the middle. 

Here is the circuit diagram for this experiment. You can enlarge the above image to view it clearly. We will be connecting one end of the Tact switch to 5V on Arduino Uno board and another end to Gnd through a 10 Kilo Ohm resistor. We will also connect this end of the switch to digital pin number 2. We have used different colored wires for clarity. Red for 5V, Black for Gnd and Yellow for pin 2.

So simple is the connection, it should not take you much long to get it connected. Once you have done the connections then we will connect the Arduino Uno board to a computer using a USB cable. 

After you have connected the Arduino board to the computer, its time to open the Arduino IDE. Now open the program given in the Examples. For this tutorial we will be using the program "DigitalReadSerial"

I have a shortened version of the same program.

There are two functions in this program. setup and loop. We start a serial communication between the computer and the Arduino board. Then we assign pin number 2 as the Input. 

In the loop function we read the input of the pin 2 with a built in function digitalRead() and then assign that value to a integer variable x. And then we display this value on the screen with the Serial.println() function.

The code in the example in Arduino's IDE is the same program but written in a more structured. I have minimized it so that we can understand it quickly.

So upload the program on the Arduino board by using the Upload menu icon. After the upload is done you can open either the Serial Plotter or the Serial monitor from the Tools menu to watch the output. Now if you press the switch, the output will change from 0 to 1. That is the digital equivalent of the value we get from the switch. 0 is for Off and 1 is for ON.

Below is a screenshot of the serial plotter. The graph rises when you press the switch and as long as you keep it pressed. Whereas in case of the Serial monitor you will see 0's on the screen and 1's when you press the switch.

This is how we can detect the state of the Tact switch through Serial communication on the computer's screen. In further projects we will try to put this feature to some good use. 

Popular posts from this blog

Introduction to Scratch Programming for ICSE Class 5

Hour of Code - Frozen with Anna and Elsa

Programming for kids - Course 4 # Artist for loops