Arduino basic usage
Arduino language
Arduino language is based on C/C + + programming language
Common functions
setup( )
When you power up the development board or press the reset button, the setup function runs once
void setup() { }
loop( )
The loop function will always run repeatedly
void loop() { }
pinMode( )
Through the pinMode() function, the pins of Arduino can be configured into the following three modes:
- Output mode
- Input mode
- INPUT_PULLUP mode (only Arduino version after 1.0.1 is supported)
grammar
OUTPUT
When the pin is set to OUTPUT mode, the pin is in low impedance state and can transmit signals outward through the circuit (if the driven motor needs more than 40mA current, Arduino will need a triode or other auxiliary components to drive them.)
void setup() { pinMode(13, OUTPUT);//Set pin 13 to output mode }
INPUT
When the pin is set to INPUT mode, the pin can be used to read the sensor signal or switch signal. (do not connect the pin with negative pressure or voltage higher than 5V.)
void setup() { pinMode(13, INPUT);//Set pin 13 to input mode }
INPUT_PULLUP
The Arduino microcontroller comes with an internal pull-up resistor. If you need to use this internal pull-up resistor, you can set the pin to INPUT_PULLUP mode through pinMode(). (do not connect the pin with negative pressure or voltage higher than 5V.)
void setup() { pinMode(13, INPUT_PULLUP);//Set pin 13 to input pull-up mode }
digitalWrite( )
Change the digital pin to HIGH [3] Or LOW [4] pattern.
grammar
digitalWrite (pin[1:1],voltage[5])
void setup() { pinMode(13,OUTPUT);//Set pin 13 to output mode } void loop() { digitalWrite(13, HIGH);//Change the voltage mode of pin 13 to high level. }
digitalRead( )
The HIGH of the digital pin can be read [3:1] Or LOW [4:1].
grammar
digitalRead (pin[1:2],voltage[5:1])
void setup() { pinMode(13, OUTPUT); pinMode(2, INPUT_PULLUP);//Pin 2 is connected to the external button } void loop() { // Read the input of pin 2 // When the switch is closed, pin 2 will obtain a low-level signal int val = digitalRead(2); // Check whether pin 2 is low // When the button is pressed, pin 2 is low and pin 13 changes to HIGH mode // When the button is not pressed, pin 2 is high and pin 13 is changed to LOW mode if (val == LOW) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } }
delay( )
The delay() function can be used to pause the program. The pause time can be controlled by the parameter of the delay() function, and the unit is milliseconds.
grammar
delay(ms[6])
void setup() { pinMode(13,OUTPUT);//Set pin 13 to output mode } void loop() { digitalWrite(13, HIGH);//Change the voltage mode of pin 13 to high level. delay(1000); // Wait 1 second digitalWrite(ledPin, LOW); // Turn pin 13 to the bottom level and turn off the LED delay(1000); // Wait 1 second }
Serial.begin( )
Set the data transmission rate (bytes transmitted per second) when the computer communicates with Arduino through serial port. The following rates can be used: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200., You can also set other transmission rates according to the device you use.
grammar
Serial.begin(speed[7])
analogWrite( )
Write an analog value into the Arduino pin. This operation can be used to control the brightness of the LED or the speed of the motor Every time Arduino executes the analogWrite() command on the pin, it will give the pin a PWM signal with a fixed frequency. The frequency of the PWM signal is approximately 490Hz
In the Arduino UNO controller, the PWM frequency of pin 5 and pin 6 is 980Hz.
grammar
analogWrite(pin[1:3], value[8])
int val = 0; void setup() { pinMode(3, OUTPUT); } void loop() { //Read the input signal of pin A0 and assign the value to the variable val val = analogRead(A0); // Convert the value read by pin A0 to 0-255 // And write the value to pin 3 analogWrite(3, val / 4); }
analogRead ( )
Used to read the value from the analog input pin of Arduino. The Arduino controller has multiple 10 digit analog-to-digital conversion channels. This means that Arduino can map a voltage input signal of 0-5 volts to a value of 0-1023.
Example:
When the input voltage of the analog input pin is 2.5 volts, the value of this pin is 512.
(2.5V / 5V = 0.51024 X 0.5? = 512)
grammar
analogRead(pin[1:4])
int val = 0; void setup() { //Arduino serial communication initialization Serial.begin(9600); } void loop() { //Read the input signal of pin A0 val = analogRead(A0); //Convert the A0 input signal to a value between 0-1023 //And it is displayed through the serial port monitor Serial.println(val); }