Thursday, July 14, 2016

Filled Under: , , , ,

Digital Read Serial

Arduino Arduino UNO R3 Basic button library

This example shows you how to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

Step 1: What You Need?

1 x Arduino Board (Arduino UNO used in this tutorial)
1 x 10k ohm resistor
1 x Button / Toggle Switch 
1 x Breadboard
Male-to-Male Jumper Wires 

Don't have components? Don't worry. Just click the component's name. 

Step 2: Build Your Circuit.


Connect three wires to the board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10k ohm) to ground. The other leg of the button connects to the 5 volt supply of Arduino board.

Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.


Step 3: Upload The Code.

1. Select the Arduino board type: Select Tools >> Board >> Select your correct Arduino board used. 


2.  Find the port number by accessing device manager on Windows. See the section Port (COM&LPT) and look for an open port named "Arduino Uno (COMxx)". If you are using a different board, you will find a name accordingly. What matters is the xx in COMxx part. In my case, it's COM3. So my port number is 3. 

Select the right port: Tools >> Port >> Select the port number.



3. You can find this code in the example of Arduino IDE.
Select File >> Examples >> 01.Basics >> DigitalReadSerial


Click press the "upload" button (see the button with right arrow mark).

In the program below, the very first thing that you do will in the setup function is to begin serial communications, at 9600 bits of data per second, between your board and your computer with the line:
Serial.begin(9600);

Next, initialize digital pin 2, the pin that will read the output from your button, as an input:

pinMode(2,INPUT);

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed, the input pin will be connected to ground through the 10k ohm resistor. This is a digital input, meaning that the switch can only be in either an on state (seen by your Arduino as a "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Once the bord has read the input, make it print this information back to the computer as a decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Arduino Software (IDE), you will see a stream of "0"s if your switch is open, or "1"s if your switch is closed.

Download: 

Arduino software

Sources:[https://www.arduino.cc/en/Tutorial/DigitalReadSerial]

Unknown

Author & Editor

My Robot Education Sdn. Bhd. (Robotedu.my) was founded in 2015 as the first robotics education centre in Malaysia to provide Arduino-based robotics courses for youths. Our vision is to be able to provide robotics education to every youth in Malaysia.

0 comments:

Post a Comment

 

  • Copyright © Arduino Tutorial ™ is a registered trademark.
    Designed by Templateism. Hosted on Blogger Templates.