[Arduino tutorial 3] Digital Read Serial-reading Digital signals from the Serial port

Source: Internet
Author: User

Reads digital signals from the serial port
This example shows how to monitor the switch status by establishing a serial port communication between the Arduino and the computer.
Hardware requirements
Arduino Development Board
An instantaneous switch, button, or mobile Switch
10 k ohm resistance
Bread
Wire
Circuit

Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Connect 3 lines to the Arduino Development Board. 2 lines in red and black are connected to the edge of the bread to provide 5 V power supply and grounding. The 3rd line is connected to the digital pin l pin 2, which is connected to one of the buttons. At the same time, this foot is grounded by a drop-down resistor (10 KB Euro. The other foot is connected to a 5 V power supply.
When the button or switch is pressed, the circuit is connected at two points. When the button is released (lifted), there is no connection between the two legs of the button. So the PIN (through the drop-down resistor) is grounded and the reading is LOW or 0 (LOW level ). Otherwise, when the button is closed (pressed), the two legs of the button are connected so that the PIN is connected to a 5 V level, reading me HIGH (HIGH) or 1.
If the number I/o pin is not connected, the LED may flash. This is because the input is uncertain. That is to say, there is no fixed connection to voltage or ground, so HIGH or LOW will be returned randomly, so you need to connect a drop-down resistor.
Schematic diagram

Code
In the following program, the first thing you need to do in the setup function is to enable serial communication between the Arduino and the computer at a rate of 9600 bits per second, as shown in this line:
Serial. begin (9600 );
Next, initialize the number pin 2, which will read the output of your button as the input:
PinMode (2, INPUT );
Now that your setup is complete, start the main loop of the program below ). when your button is pressed, the 5 V voltage flows through your circuit, and when the button is released, the input pin will be grounded through a 10 k resistor. As a digital input, the switch can be either on or off (in Arduino as 1 or high) or off (in Arduino as 0 or low), with no other options.
The first thing you need to do in the main loop is to create a variable to save the information obtained from your switch. Because this information can only be "1" or "0", you can use an int data type. Name this variable sensorValue and assign it any value obtained from the number pin 2. The downstream code completes this task.
Int sensorValue = digitalRead (2 );
Once Arduino reads the input, it displays the information in numerical form on the computer. The Serial. println () command can be done. This is our last line of code:
Serial. println (sensorValue );
Now, the serial port monitor is enabled from the Arduino environment. When the switch is released, you can see a series of "0". When the switch is closed, there will be a series of "1 ".
/*
Reads digital signals from the serial port
Read the digital input from pin 2 and print the result to the serial port monitor.
 
This routine can be made public.
*/

// Pin 2 is connected to a button to give it a name:
Int pushButton = 2;

// Execute the following command when reset is pressed During setup:
Void setup (){
// Initialize the speed of serial communication to 9600 bits per second:
Serial. begin (9600 );
// Use the pin of the connection button as the input:
PinMode (pushButton, INPUT );
}

// The loop process will always run:
Void loop (){
// Read the Input Pin:
Int buttonState = digitalRead (pushButton );
// Print button status:
Serial. println (buttonState );
Delay (1); // pause between the read values to maintain stability
[Get Code]
See Also:
Setup ()
Loop ()
PinMode ()
DigitalRead ()
Delay ()
Int
Serial
DigitalPins
BareMinimum: The bare minimum of code needed to start an Arduino sketch.
Blink: Turn an LED on and off. www.2cto.com
DigitalReadSerial: Read a switch, print the state out to the Arduino Serial Monitor.
AnalogReadSerial: Read a potentiometer, print it's state out to the Arduino Serial Monitor.
Fade: Demonstrates the use of analog output to fade an LED.
ReadAnalogVoltage: Reads an analog input and prints the voltage to the serial monitor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.