1th section Select Arduino Development Board 1.1 what is Arduino?
For the Arduino, the official has a bunch of explanations.
As a software program ape, in My eyes, Arduino is a convenient way to learn "scary hardware". It hides complex hardware names, attributes, and only requires some simple software knowledge to learn hardware development.
1.2 How to choose the Arduino Development Board 1.2.1 official version or compatible version
Arduino is open source project, hardware structure, software design are open source.
So there is no problem of piracy.
The official version of the quality is certainly the leverage, but the price is also expensive.
The two prices are about the same, one selling renminbi and one selling dollar.
not bad money to buy the official version, otherwise the compatible version is also good.
1.2.2 Uno or Mega
Arduino has a range of development boards for various purposes, the most common of which are two, Uno and Mega.
Uno prices are cheaper and feature simple.
Mega price than Uno more than 20, the official version of more than $20, compatible version of more than 20 yuan.
We buy Arduino Development Board, the main purpose is to learn, requirements can easily learn more features.
Uno interface too few, such as a car, after four motors, the basic can not add other modules.
If you want to make a Bluetooth car, debugging the nightmare will come. Because Uno has only one serial port, Bluetooth and USB are shared.
You first unplug the Bluetooth module to upload the program, after uploading, and then plug in the Bluetooth cable to start testing.
If you do not just study, but to develop products, to consider the cost of the product price ...
There's no mistake in choosing Mega, you can now develop on mega and finally do performance testing on Uno.
So, my advice is to choose Mega.
The 2nd section starts with HelloWorld
I remember to start with the C language and write a HelloWorld program first.
Arduino let's start from here, too.
2.1 Building the development environment
Download the Arduino Integrated Development Environment (hereafter referred to as the Arduino IDE).
After extracting, double-click the Arduino.exe in this directory to launch the Arduino IDE.
I am here to take the Windows platform as an example, Mac,linux interface, operation is basically the same, you can refer to this example to learn.
2.2 New Project
Launch the Arduino IDE and click New (shortcut key CTRL + N) on the menu file to create a new project.
Click the menu file--Save (shortcut key Ctrl+s) to save the generated project.
File name Here I fill in "HelloWorld", the Arduino IDE generates a folder named "HelloWorld" and generates a Helloworld.ino file with the same name in this folder.
The. Ino file is the main file for the Arduino project, and theArduino requires that the Ino file be placed under a folder with the same name .
2.3 Hello World
The newly generated project automatically generates setup() and loop() two functions.
We change the code first, output "Hello world", a bit of a personal feeling. We'll explain the code structure later.
Set the serial baud rate in the Setup function
void setup() { // put your setup code here, to run once: Serial.begin(9600);}
Output "Hello world!" with serial port
voidloop() { // put your main code here, to run repeatedly: Serial.println("Hello world!");}
Click the button in the upper right corner to open the serial monitor (shortcut key ctrl+shift+m)
The baud rate must be the same as set in Setup, this example is 9600.
You can see the constant output of "Hello world! ”
2.4 Code structure
Helloworld.ino file on two functions
void setup()voidloop()
How do they work?
Look at the code below.
void setup(void);voidloop(void);int main(void) { setup(); while(1) {} loop(); } return0;}
is not very clear.
Where does the code go in main and while? The Arduino IDE is hidden and makes our code structure clearer.
setup()Execute only once, function to put the initialization
loop()Continuous loop execution, used to check the status of each sensor, the driver runs.
2.5 Serial
The Arduino IDE communicates with the PC (or other device) via the serial port.
The serial in the Arduino IDE is the avatar of the serial port.
note that serial is a system object, not a class , so it is used directly serial.begin ().
2.5.1 Official website details
Details can be described in the official website:
Https://www.arduino.cc/en/Reference/Serial
Learn about Arduino and visit our website at any time.
2.5.2 Serial hardware structure
Uno Example
USB interface and 0, 1 pins are all connected serial
Mega Example
USB interface and 0, 1 pins are all connected serial
18, 19 pins are connected Serial1
16, 17 pins are connected Serial2
14, 15 pins are connected Serial3
3rd Section control LED light switch
Arduino comes with a lot of examples.
Blink is one of the few examples where you can start learning with the Arduino Development Board.
Menu file, example->01.basic->blink open the example you want to talk about today.
3.1 Setup function
setup() { // initialize digital pin 13 as an output. pinMode(13, OUTPUT);}
Learning hardware development, a big hurdle is to remember many complex register addresses.
The point of the Arduino is that she simplifies many of these addresses and defines several different types of hardware sockets, called Pins.
You only need to do these types of pins when you are programming.
Today, let's talk about the most common pins, digitalpins.
The official website is explained as follows:
Https://www.arduino.cc/en/Tutorial/DigitalPins
Simple is a pin that can input or output digital signal 0,1.
Whether a pin is used for input or output signals is defined by the Pinmode function.
pinMode(13, OUTPUT);
This code means the pin with the number D13, which is defined as the output mode.
3.2 Why is a D13 pin?
Because the D13 pin is connected to an LED light that is soldered to the Arduino Development Board.
So, if there is no external LED module, use the D13 pin to do the demonstration.
3.3 Loop function
loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second}
Loop function total 4 lines of code
digitalWrite(13, HIGH);
Put the D13 high, that is, power on.
Remember that only the PIN is set to output,digitalwrite before it takes effect.
delay(1000);
Delay 1 seconds, in this 1 seconds, the LED light state is unchanged, that is, lit state.
digitalWrite(13, LOW);
Put the D13 low, that is, power off.
delay(1000);
Delay 1 seconds, in this 1 seconds, the LED light state is unchanged, that is, off the state.
After the loop function is executed again, the phenomenon we see is that the LED light is a second light, a second dark flicker.
Where is the LED light controlled by the 3.4 D13?
Different development boards, D13 control of the LED light position, color are slightly different.
I believe most crossing me in the next, is a software origin. I can't tell what the LED lamp looks like: P
The board is connected to the power supply (USB cable computer), if only one led light on, the LED lamp must have an "on" mark, this is the power to switch on the LED light.
After uploading this example to the development Board, the flashing one is that we are going to find the D13 LED light. You can see an ' L ' mark next to this led light.
The 4th section controls the brightness of the LED light
LED lights are the most common Arduino module, the previous section we talked about control of the Arduino Development Board comes with LED lights.
This section also speaks of LED lights, speaking of external, controllable brightness of the LED lights.
4.1 PWM
First introduce a concept, Pulse Width modulation referred to as PWM.
The digital interface can only output 0 or 12 states, and PWM generates a value between 0 and 1 by quickly switching between 0 and 12 states.
For example, in 1000ms, the average 400ms is 1 (high), the average 600ms is 0 (low), the two states are evenly distributed, the output of a value of 0.4.
Details to participate in the official website:
Https://www.arduino.cc/en/Tutorial/PWM
4.2 LED Module
Your LED module might be like this.
It could be like this.
No matter what the length, as long as the monochrome dimmable led, are three wires.
The callout character in is the control line, VCC is the power cord, and GND is the ground.
4.3 Connection module
Mega Development Board, 2-13-pin support PWM, any choice can be.
The UNO Development Board, the 3,5,6,9,10,11 PIN supports PWM, and can be seen on the development Board with a ' ~ ' on the digital side of the pin.
The LED light in is connected to the Mega D2 pin; VCC is connected to the Mega 5V pin; GND is connected to the Mega GND pin.
Early Development Board, in order to prevent voltage instability breakdown of electronic devices, the need to connect a resistor.
Now the development Board, only when the motor to increase the resistance, the usual practice is to add a piece of expansion board.
4.4 Modifying the Code 4.4.1 modifying the Pin
Because this time we used the D2 pin, so we need to change the 13 here to 2
pinMode(13, OUTPUT);
And I want to change the 13 here to 2.
loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second}
Click on the menu item, upload, upload the code to the Development Board.
You can see that this external led light flashes as well as the built-in LED lights.
4.4.2 Optimization Code
Just now we have modified 3 code because of the replacement pin.
In Arduoni development, the replacement of pins is a very common occurrence, and the usual practice is to add a macro definition before the Setup function
2void setup() {...
Replace the D2 related code in your code with Led_pin, and it's convenient to replace the pin later.
4.5 Adjust led brightness 4.5.1 adjust to fixed value
Using the Analogwrite function to control the PWM pin, the first parameter is the PIN number, the second is the output to the PIN value, the range 0~255.
Analogwrite (Led_pin, 10) This line of code makes the LED light emit faint light.
voidloop() { 10);}
4.5.2 Dynamic adjustment of brightness
We use the serial port to enter a number between 0-255 to adjust the LED light brightness.
In this example we will demonstrate how to use the four serial object functions.
4.5.1.1 initialization
Use begin() the function to set the serial baud rate in the setup function.
void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT);}
4.5.1.2 Detecting the serial port for data input
Serial.available()Returns true to indicate that the serial port has data to be read
void loop() { while (Serial.available()) { ...
4.5.1.3 reads a long number from the serial port
Serial.parseInt()Reads the string entered in the string. If the input is non-numeric, it will be resolved to 0.
voidloop() { while (Serial.available()) { Serial.parseInt();
4.5.1.4 Change LED light brightness
analogWrite()function is used to write data to the analog interface.
The first parameter specifies the pin ordinal, and the second parameter specifies the value of the write pin.
long10;void loop() { while (Serial.available()) { ledLevel = Serial.parseInt(); } analogWrite(LED_PIN, ledLevel);}
Because Analogwrite accepts values between 0-255, the code can do some optimizations.
Replace the long type with uint8_t.
10;voidloop() { while (Serial.available()) { ledLevel = Serial.parseInt(); } analogWrite(LED_PIN, ledLevel);}
4.5.1.5 Output Debug Information
Print output parameter information in serial port.
println output parameter information in serial port, and enter.
10;void loop() { while (Serial.available()) { ledLevel = Serial.parseInt(); Serial.print("ledLevel="); Serial.println(ledLevel); } analogWrite(LED_PIN, ledLevel);}
4.5.1.6 serial port emulator settings modification
The serial port simulator is modified slightly, and the lower right corner is changed to "no Terminator".
Otherwise, the parseint will parse the terminator into 0, causing the led to go out incorrectly.
4.6 Testing
/*******************************************************************/
* Copyright Notice
* This tutorial is only published in CSDN and the green Bean Network, other websites appear this tutorial is infringing.
* In addition, we have introduced the Arduino smart hardware related tutorials, you can in our online shop and I learn Arduino programming to purchase the relevant hardware. At the same time, we also thank you for the support of these yards of farmers.
* Finally, thank you again for 安豆 your support, thank you:)
/*******************************************************************/
Teach you to do the Bluetooth car (a)