Resume
It has been a long time that I wanna write an article for Arduino. So, let’s rock n roll.
Introduction
Arduino is a computer hardware and software company, project, and user community that designs and manufactures micro-controller kits for building digital devices and interactive objects that can sense and control objects in the physical world.
From Wiki-Arduino
So, we can actually affect our physical world by programming!
Connecting to Arduino
In this article, we will use Arduino Uno as our experimental platform.
First, start pining your Arduino Uno to your computer and open Arduino IDE.
In some situations, Arduino IDE cannot recognize what you’ve connected. Just click on Tools tab -> Board -> Arduino/Genuino Uno
to select Arduino Uno. Then select connected Arduino Uno by clicking Tools tab -> Port -> The port that you've connected to
.
Coding preparations
setup() & loop()
setup()
runs once when we start our program. So that pre-configurations should be put here. Attention, as setup()
is a function, so every variables defined here will be lost after setup()
is finished.
loop()
runs one after another. So that our main program should be written here including detecting data from sensors, controlling motors, etc.
Baud rate
When connecting to a physical device, we have to indicate its’ baud rate which means that bits (or symbols) transferred per second. If we have mis-set this value, data transferred will be weird.
We can set baud rate by the following codes:
const unsigned int BAUD_RATE = 9600;
void setup() {
Serial.begin(BAUD_RATE); //Init serial
}
As we can see, Serial.begin(BAUD_RATE)
has been written in setup()
. It’s mainly because Arduino is a resource limited device, variables, assignments, function calls may cost additional resource in static area.
Reading data
Analog
Digital
Writing data
PWM
Analog
Digital
Example
Here’s an example of turning servo and lighting up a LED bulb.