Introduction
Introduction
An Introduction
To Arduino
[Link]
01
DFRobot 01. An Introduction to Arduino
01
What is Arduino?
Arduino is an open-source hardware and software platform designed for computer programmers, industrial artists, professionals and those
interested in developing interactive devices and applications specific to an interactive development environment.
Arduino can receive input signals from various sensors and inputs. By controlling light sources, motors, or other actuators, Arduino can change
the surrounding environment. Programs for the microcontroller on the Arduino board are written in Arduino’s programming language (based on
“Wiring” - an open source framework for microcontrollers) and run in the Arduino development environment (based on “Processing” - an
open source programming language and integrated development environment).
Arduino is able to run independently or communicate with software running on a computer (for instance, Flash, Processing and MaxMSP).
The open-source Arduino IDE, which is free to download, makes it easy for you to write code, upload it to the board and come up with your
own interactive devices.
Introduction to DFRduino
In these lessons we will use DFRobot’s own clone of an Arduino Uno board: “DFRduino Uno”. DFRduino operates in exactly the same way
as an Arduino Uno. To save confusion with names, let’s just call it the microcontroller. Let’s examine its features:
Sections with annotations below are parts that will be regularly used. Digital pins and analog pins marked on the diagram are what we call I/O
(input/output). Digital pins are numbered from 0 to 13. Analog pins are numbered from 0 to 5.
Voltage supply is also shown on the diagram below. DFRduino can be powered either by a USB connection or through a 6~12V DC supply
via the barrel jack connector. Also integrated in to the board are four LED lights and a reset button. The LED light marked with “ON” is the
power indicator, which will be on once power is connected. The LED light marked with ”L” is an indicator for digital pin 13 which will be dis-
cussed in the next section. TX (transmit) and RX (receive) are indicator lights for serial communication. When we upload a program, these
two lights will blink rapidly, showing that data is being transmitted and received between the board and your computer.
USB Port
Power Indicator
DFROBOT
DFRduino
UNO v3.0[R3]
D13 Pin Signal Indicator
Port Communication Indicator
DC Power Jack
(6~12V)
2. Install Drivers
Connect the microcontroller to your comput-
er using a USB cable. Once connected, the
board’s power indicator light will turn on.
[Link]
Inside your Arduino directory, open “[Link]”. The application If you wish to change the language of the interface, select “File” and
will open and the code editing interface will appear. then “Preferences” to open preferences.
The dialog box shown below will pop up. Select “Editor Language”,
choose your language and then click OK.
Close Arduino IDE and then reopen it for the changes to take effect.
DFRobot
01. An Introduction to Arduino
08
We will use Arduino IDE a lot in these tutorials, so lets learn about its features:
Verify
Upload
Serial Monitor
Save
Open
New
Coding Area
Compiler Window
Arduino IDE allows you to edit and upload programs to your microcontroller. Arduino IDE
calls programs “sketches”, but in these tutorials, we will simply say “program” or “code”,
rather than “sketch”, but they really mean the same thing.
The interface shown in the picture above is where you’ll write and upload code within Arduino
IDE. The main white area is where you input code. We use the C programming language to
program the microcontroller, which we will discuss in more detail in later chapters. When you
press “verify” or “upload” , the code instructions you have written will be translated in to
machine language by a piece of software called a “compiler” so that the microcontroller
can understand it. This process is called “compiling” or “verifying”. Messages shown in
the black area will show information about the compiling and upload process.
DFRobot
01. An Introduction to Arduino
09
4. Upload a Blink Program
Let’s upload a simple program to become familiar with the uploading process and to test the microcontroller. This code will turn the “L” LED
light on the microcontroller on and off. The location of the “L” LED is circled in the diagram below.
As this is an example program, there will be no syntax errors in the code. When we write
code ourselves we can click “Verify” to check if there are any syntax errors. Try it now.
The code is run through the compiler. This green bar shows the compiling process.
Before code is uploaded to the microcontroller, the IDE will automatically verify it.
If there are any errors in your code, a big ugly orange message box will appear above the
compiler window indicating a problem, and your code can not upload until it is fixed!
DFRobot
01. An Introduction to Arduino
11
Before we can upload a program, we need to tell the computer where to send it. In Arduino IDE, we
need to select which type of Arduino board we are using as well as the serial port that the board is
connected to.
Navigate to Tools > Board and then select Arduino UNO from the list. That’s it!
Now select the serial (COM) port. Navigate to Tools > Port and your available serial ports will
be listed. Usually this will be “COM” followed by a number. Select the available port.
In our example the board is connected to the computer on COM36.
DFRobot
01. An Introduction to Arduino
12
We should now be able to upload code to the microcontroller.
Click upload in Arduino IDE. If all is well, the code will verify and then upload to the microcontroller.
When the upload has completed successfully, the microcontroller will automatically reset
and the code will execute. Is the light blinking?
1. Verify code
2. Select board and COM port
3. Upload!
If you have any questions or comments about this tutorial, or any other tutorials in
this guide, feel free to contact us on the DFRobot forum:
[Link]
The DFRobot forum is a community where hackers and makers can share their
exciting ideas. Come and visit to get ideas about what you can make with your
DFRduino microcontroller, or just share what you’ve done following these tutorials.
We love your feedback!
Lesson 2
How Does a Device
“Think”?
[Link]
02
DFRobot 02. How Does A Device “Think”? 01
Simple Automatic Control Devices
Any device that includes a micro- Let’s draw an analogy between an automatic As well as our microcontroller, we simply
controller can be considered a simple control device and a person. In a person, sig- need an acoustic sensor and a buzzer. The
automatic control device. There are nals are sensed through sight, hearing, taste, acoustic sensor “hears” a sound, the
three essential constituent parts for a touch and smell, and then processed by microcontroller receives a signal and then
simple automatic control device: the brain which then outputs responses to outputs a signal to the buzzer for it to buzz.
An input unit for collecting signals each. In this case, the output signal would In this case the acoustic sensor is the
An output unit for sending out signals be the actions of that person. input unit; the microcontroller is the control
A control unit for processing the sig- unit and the buzzer is the output unit.
A microcontroller can output responses as
nals received (such as a microcontroller). well. Output responses include sound, light
and movement (DC Motors, LEDs, servos
etc.)
Think!
Here is another way to explain it: imagine
someone says “hello” to you, and you
Can you tell which components in the
Output Intput immediately reply “hello” in return. In this
kit can be used as input units and
case, your ear is the input unit; your brain is
which can be used as output units?
the control unit and your mouth is the output
unit.
Control
hardware. In the context of our person analogy, hardware is the communicate by signals, which in turn are processed by code.
body of our device. However, the brain is much more important as How do input units and controllers communicate with each
it produces ideas and then controls actions every person takes. other? How do controllers communicate with the output units?
Code here functions as the mind of a person. Both body and mind To answer the above questions, we first need to understand two
HIGH is a 5V signal and represents “1” (or on). are digital or analog:
LOW is a 0V signal and represents 0 (or off). (1) If a sensor has a green wire it uses digital signal; if a
sensor has a blue wire it uses analog signal.
(2) “A” or “D” may be marked on the sensor’s board. “D” rep-
Analog Signals:
resents “digital” and “A” represents “analog”.
An analog signal has a range of values.
SD Module Headers
Black: GND
3.5V/5V Power Output Switch Red: VCC
Blue: Analog Pins(0~5)
Run/Upload Switch
The advantage of the I/O expansion shield is that there are more power and ground pins than those on the bare microcontroller board. This
gives you enough power pins to connect various sensors at the same time.
On the expansion shield, there are a row of power pins in red and a row of GND pins in black below the digital pins.
If you would like to know more about the sensor expansion shield, please
Different colors shown in our DF Kit have different
meanings: refer to the DFRobot wiki for information:
Red = Power This session gives you a general idea of what makes our devices work.
Are you excited to get your devices going? You’ll get a chance to in the next
Black = Ground
lesson!
Lesson 3
Analog and Digital
Signals
[Link]
03
DFRobot 03. Analog and Digital Signals 01
Let's begin!
Let’s draw an analogy between microcontrollers and people
Signals, such as analog and digital signals, are the microcontroller’s “nerve impulses”.
In this session, we will explore the differences between analog and digital signals in more
detail.
DFRobot 03. Analog and Digital Signals 02
Digital Signals
We will use a digital button module to demonstrate a digital signal. This is included in your kit.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
x1 x1
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
RUN SS RXD
SCK
PWR_IN
MISO
3V3 5V
RST 0 1 2 3 4 5
I/O Sensor Expansion
GND
VINGND
VCC
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1
D
Digital Push
Button Module
Digital Push Button
DFRobot 03. Analog and Digital Signals
03
Connections
Take the Sensor Expansion Shield and stack it on top of your microcontroller – make sure that the pins are correctly aligned to avoid damage
to the shield.
Take the Digital Push Button module and connect it to Digital Pin 2 (be sure that the power, ground and signal connections are correct or you
risk damaging your components!)
See the diagram below for further details:
D
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
When the connections are made, connect the USB cable. This will power on the microcontroller. We can now prepare our program to upload.
DFRobot 03. Analog and Digital Signals
04
Serial Port Monitoring
Open Arduino IDE and select: File > Examples > [Link] > DigitalReadSerial
The following code should appear:
int buttonState = digitalRead(pushButton); // record statistics about the status of digital pin 2
[Link](buttonState); // Serial port print statistics about the status of digital pin 2
Click “Upload”. Arduino IDE will verify the code and then upload it to The serial monitor shows information from the microcontroller. We
your microcontroller. are going to use it to view the status of the button. You should see
Once the upload is complete, open the serial monitor. To do this, “0” appearing continuously. This indicates that the button is off
navigate to Tools > Serial Monitor, or click the magnifying (not being pressed).
glass icon on the top right hand corner of the toolbar. If you press the button, you will see “1” appear for as long as the button
is pressed. This indicates that the button is on (being pressed).
Serial Monitor
The baud rate is the data speed that the serial port communicates. It
needs to be set to 9600 baud.
“0” and “1” or off and on are the two values of digital signal. We can
use this in various ways with our devices.
DFRobot
03. Analog and Digital Signals
05
Analog Signal
In this section we are going to explore analog signal in more
detail. We are going to use a sensor which uses analog val-
ues: a rotation sensor.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
x1 x1
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
RUN SS RXD
SCK
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1
Analog Rotation Sensor
A
Rotation Sensor-L
DFRobot
03. Analog and Digital Signals
06
Connections
Connect the rotation sensor to Analog Pin 0. Make sure that signal, ground and power are connected to the correct corresponding pins, just
like in the previous session. Connect the USB cable to the microcontroller to power it on. We can now upload a program.
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
A
Rotation Sensor-L
Code Input
In Arduino IDE, navigate to File > Examples > [Link] > AnalogReadSerial
Click “Upload”. Arduino IDE will verify the code and then upload it to the microcontroller.
Once the upload is complete, open Arduino IDE’s serial port monitor. To do this, navigate to Tools > Serial Monitor
Set baud rate of the serial port to be 9600 baud.
A series of values will flash in a column in the serial monitor.
Try to turn the rotational sensor. A figure between 0 and 1023 will show up, depending on how far the rotational sensor’s shaft is rotated. 0
means no rotation and 1023 means a full rotation.
DFRobot
03. Analog and Digital Signals
08
We use the Serial Monitor to view information from the microcontroller. We can also use it to send information back to the microcontroller, and
interact in with it in various ways while it runs a program.
DFRobot
03. Analog and Digital Signals
09
Code Differences
If you examine the code in detail, you can see that digital pins and analog pins are read using different commands.
Digital Pins:
Analog Pins:
Try It Yourself
The sensors in your DFRobot kit connect in much the same way as the ones demonstrated. Just make sure your
power, ground and signal connections are correct. Once connected, you can check the serial monitor for output and
see what values they output. Try to experiment and have fun!
Lesson 4
Make an LED Blink
[Link]
04
DFRobot 04. Make an LED Blink 01
Make an LED Blink
We’ve given you a brief idea of what Arduino is and how it works, as well as some theory behind digital and analog signals. Now let’s
start making some real stuff. We will start with the basics: how to make an LED blink.
In the previous session, we’ve used the blink program to test the microcontroller. This time we’ll attach our own LED to digital pin 13 instead
of using the microcontroller’s integrated LED.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1
Digital piranha LED-R
Connect the Digital Piranha LED-R module to digital pin 13 on the microcontroller. Make sure your power, ground and signal connections are
correct or you risk damaging your components.
When all your connections are made, connect the microcontroller to your computer via the USB cable.
Digital piranha LED-R
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
DFRobot 04. Make an LED Blink 03
Hardware Analysis (Digital Output)
In the previous sessions, we’ve mentioned inputs and outputs. The device we will make is
composed of two parts: a control and an output. The microcontroller is the control unit and
the digital Piranha LED-R module is the output unit. No input unit is included in this particular
device.
GND
13
IOREF 12
RESET ~11
3V3 ~10
POWER
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
VIN
7
~6
D
A0 ~5
4
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
Once the code is input and you are happy with it, click “Verify” in Arduino IDE. The IDE will check
the code for errors. If there are no errors, you are ready to upload. Click “upload” and Arduino
IDE will upload the code to your microcontroller. When the upload is complete, your external
LED will blink!
The code above shows that ledPin is set up as the output mode.
This part of the code is executed when the device is initialized. It
The comma in the brackets is the code syntax and needs to be there
runs only once. It is used to declare outputs and inputs of the
to separate the parameters.
device.
pinMode(ledPin, OUTPUT);
void loop() {
The code above shows that ledPin is set up as the output mode.
This part of the code is executed after void setup(){. The comma in the brackets is the code syntax and needs to be there
Once initialized, it runs in a loop forever (or until the device is to separate the parameters.
powered off or a component fails). This is where all the key argu-
What is ledPin?
ments of the code are contained.
Examine the first line of the code below:
int ledPin = 13;
digitalWrite(ledPin,HIGH); will turn on for 1 second and then turn off blink for 250 milliseconds
digitalWrite(ledPin,LOW);
//LED off You might notice “//” and “/*…*/”
located ahead of the code:
[Link]
05
DFRobot 05. Sensor Light 01
Sensor Light
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
DFRduino UNO
GND
I/O Expansion Shield V7.1
VINGND
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
VIN
A0
A1
A2
A3
A4
A5
5V
x1 x1
Digital piranha LED-R
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
BT/APC UNO v3.0[R3]
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
DFRobot 05. Sensor Light 03
Hardware Analysis
This device is composed of three parts: an input unit, a control unit and an output unit. The IR
sensor is the input unit; the microcontroller is the control unit and the Digital Piranha LED-R is the
output unit.
The IR sensor and LED use digital values, so both should be attached to a digital pin.
GND
13
IOREF 12
RESET ~11
3V3 ~10
POWER
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
VIN
7
~6
D
A0 ~5
4
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
int ledPin = 13; // Digital Piranha LED-R to be attached to digital pin NO.13
int sensorState = 0; // variable sensorState for storing statistics about the status of the sensor
void setup() {
pinMode(ledPin, OUTPUT); // LED is the output unit
pinMode(sensorPin, INPUT); // Sensor is the input unit
}
void loop() {
sensorState = digitalRead(sensorPin); // Read statistics collected from the sensor
The IR sensor is the input unit; the LED is the output unit. They are initialized in the setup() function.
We can read values from digital pins using the function digitalRead(). This can be written in the loop() function.
sensorState = digitalRead(sensorPin);
We briefly touched upon comparison change the interval of the LED flash-
operators above. Let’s examine them in ing to make them quicker or slower
We’ll use the if statement here:
more detail: for different effects. It would be great
}
}
Lesson 6
Mini Lamp
[Link]
06
DFRobot 06. Mini Lamp 01
Mini Lamp
A lamp is something we use on a daily basis, usually made up of a light source and a simple switch. Let’s make our own.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
PROG
SS
SCK
MISO
RXD
I/O Sensor Expansion
3V3 5V
RST 0 1 2 3 4 5
(with USB cable) Shield V7.1
PWR_IN
GND
VINGND
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1 x1
Digital piranha LED-R
D
D
D
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
DFRobot 06. Mini Lamp 03
Hardware Analysis
(Digital Input – Digital Output)
The digital push button is the input unit and the LED is the output unit. Like our last
experiment, we have one digital input and one digital output.
AREF
RX
TX
GND
13
IOREF 12
RESET ~11
3V3
D
~10
POWER
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
D
7
VIN
~6
A0 ~5
4
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
int buttonPin = 2;
int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}
The button is the input device and the LED is the output device. Wait for another 50ms and check if the button’s signal has the same
pinMode(buttonPin, INPUT); value with its current state. If not, change the state of the button. If
pinMode(ledPin, OUTPUT); the button is in the state of HIGH (meaning the button has been
pressed), change the state of LED.
To avoid signal errors such as this, we will wait when the signal
changes and read it some time after.
The function millis() will record the time when data
Exercise
collected has changed
People are always listening to music on their head-
if (reading != lastButtonState) {
phones. This means that if they are at home and the
lastDebounceTime =
doorbell rings, they cannot hear it.
millis();
A doorbell with a mini lamp would solve this problem. If the
}
doorbell is pressed, the lamp will light up. Why not try and
add this feature to your own doorbell at home?
millis() is a function which measures duration. It is measured
in milliseconds (1 second = 1000 milliseconds). This particular
function will start counting when the microcontroller is
powered on.
Lesson 7
Sound Activated LED
[Link]
07
DFRobot 07. Sound Activated LED 01
Sound Activated LED
In this session we will make a sound activated LED. You will be able to make an LED flash by clapping your hands. To make it work,
we need a sound sensor. We can use a sound sensor to make various interactive and sound control devices besides this
experiment.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
RUN SS RXD
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1 x1
Analog Sound
Digital piranha LED-R
Sound V2
MAX
DFRobot 07. Sound Activated LED 02
Connections
Analog Sound
Digital piranha LED-R
Sound V2
D
MAX
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
Be sure that your power, ground and signal connections are correct
or you risk damaging your components.
In the previous sessions we have talked about analog and digital signals. The analog sound
sensor is, as the name suggests, an analog sensor.
AREF
RX
TX
GND
13
IOREF 12
RESET ~11
3V3 ~10
POWER
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
D
7
A VIN
~6
A0 ~5
4
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
void setup() {
pinMode(ledPin, OUTPUT);
// [Link](9600); // You can uncomment this for monitoring
}
void loop(){
int soundState = analogRead(soundPin); // Read sound sensor’s value
When you have finished, click “Verify” to check the code for syntax errors. If the code
verifies successfully, you can upload it to your microcontroller.
Once the code has uploaded, try clapping your hands. The LED should flash on.
When there is no sound, the LED should remain off.
DFRobot 07. Sound Activated LED 05
Code Review
The reason is because all analog pins are in INPUT mode by default, so we don’’t need to set them!!
The sound sensor is the input unit, and it should read from analog pin 0. The function for this is:
analogRead(pin)
This function is used to read the analog pin. Your microcontroller’s analog pins are attached to an A/D converter (analog to digital
converter - a special chip on the microcontroller board), so voltage between 0 and 5V will be mapped to a number between 0 and
1023. Each number collected represents a voltage. For example, 512 = 2.5V
...
}else{
... }
You can open the serial monitor and test out a threshold value that best represents the sound volume you need. Then you can adjust the
value in the code accordingly.
Lesson 8
Fading LED
[Link]
08
DFRobot 08. Fading LED 01
Fading LED
In previous sessions we’ve learned how to turn an LED on and off. In this project we’re going to make a fading LED. The LED will go from dim
to bright and bright to dim continuously.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
DFRduino ON
L *PWM
UNO v3.0[R3] BT/APC
Expansion
SD GND
[Link]
5V
V7
5V
I/OShield
GND DTR
ICSP MOSI TXD
PROG
SS
SCK
MISO
RXD
I/O Sensor Expansion
3V3 5V
PWR_IN RST 0 1 2 3 4 5
(with USB cable) GND
Shield V7.1
VINGND
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1
Digital piranha LED-R
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
Be sure that your power, ground and signal connections are correct or you risk damaging your components.
When you have connected the components and checked your connections, connect the microcontroller to your PC with the USB cable
so you can upload a program.
DFRobot 08. Fading LED 03
Hardware Analysis
(Analog Output)
This device is similar to experiment 1 where we made an LED blink. We also only have one
output unit. The difference in this project is that we are using the LED for analog output,
rather than digital output. You can refer to the code section for more information about this.
AREF
RX
TX
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
7
VIN
~6
A0 ~5
4
D
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
void setup() {
pinMode(ledPin,OUTPUT);
}
void loop(){
for (int value = 0 ; value < 255; value=value-1) {
analogWrite(ledPin, value);
delay(5);
}
for (int value = 255; value >0; value=value-1){
analogWrite(ledPin, value);
delay(5);
}
}
When you have finished, click “Verify” to check the code for syntax errors. If the code verifies
successfully, you can upload it to your microcontroller.
Once the code has uploaded, you should see the LED gradually growing brighter and then
dimming.
DFRobot 08. Fading LED 05
Code Analysis
} There are six digital pins on your microcontroller that are marked with
The Condition Is
the symbol “~”, which means they can send out a PWM signal.
Evaluated
Let’s explore PWM in more detail by examining these five Orange vertical lines on this diagram represent an interval.
square waves: Black lines represent the electric pulse.
Each value included in the analogWrite(value) function
Pulse Width Modulation can be mapped to a certain percentage. 0 = 0% and 255 = 100%.
0% Duty Cycle-analogWrite(0)
The duty cycle refers the length of time the pulse goes from LOW
to HIGH and HIGH to LOW.
The duty cycle of the first square wave is 0% and the correspond-
25% Duty Cycle-analogWrite(64) ing value is 0.
50% Duty Cycle-analogWrite(127) If the duty cycle is 100%, the analog value will be 255.
The longer the duty cycle, the brighter the light will be.
75% Duty Cycle-analogWrite(191) PWM’s electronic applications include adjusting the brightness of an
LED, controlling the speed of a DC motor, and many others. Can you
With the same hardware connections we’ve used in this session, the
LED can achieve completely different effects by using a different code.
Have a play with the code and see if you can make different lighting
effects. Use your imagination and have fun with it!
Lesson 9
Light Regulator
[Link]
09
DFRobot 09. Light Regulator 01
Light Regulator
A light regulator is a device with which you can control brightness of a light. In this session, we’ll show you how to control the brightness of an
LED with an analog rotation sensor (also known as a potentiometer). Using this, the light will change from dim to bright and bright to dim
depending on the rotation of the sensor. The greater the rotation angle, the brighter the LED, and vice versa.
The rotation sensor can also be used to control the rotation angle of a servo or the speed of a DC motor. You can find many uses for the
rotation sensor.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
SERVO_PWR
RX
DFRduino ON
LL *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
Expansion
GND
[Link]
SD
[Link]
5V
Shield V7
5V
V7
5V
5V
I/OShield
GND
GND DTR
DTR
MOSI
MOSI TXD
TXD
ICSP
VCC
VCC
OUT
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1 x1
Digital piranha LED-R
Rotation Sensor-L
A
DFRobot 09. Light Regulator 02
Connections
Be sure that your power, ground and signal connections are correct or you risk damaging your components.
When you have connected the components and checked your connections, connect your microcontroller to
your PC with the USB cable so you can upload a program.
Digital piranha LED-R
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
SERVO_PWR
RX
L *PWM
DFRduino ON
SD GND
[Link]
5V
Shield V7
5V
Rotation Sensor-L
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
A
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
DFRobot 09. Light Regulator 03
Hardware Analysis
(Analog Input-Analog Output)
In the last session we learned how to achieve analog output with a digital pin using PWM.
In this session, we are going to control analog output using analog input.
The rotation sensor is the input device. The LED is the output device. Both input and output
devices use analog values.
AREF
RX
TX
GND
13
IOREF 12
RESET ~11
3V3 ~10
POWER
5V
DFRduino
UNO v3.0[R3]
~9
DIGITAL (PWM~)
A VIN
7
~6
D
A0 ~5
4
ANALOG IN
A1
A2 ~3
A3 2
ON
A4 TX 1
ICSP
RX 0
A5
void setup() {
pinMode(ledPin, OUTPUT); }
void loop() {
int sensorValue = analogRead(potPin); // collect value at analog pin No.0
// A value between 0 and 1023 to mapped to a value between 0 and 255
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, outputValue); // write corresponding value for LED
delay(2);
}
When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully,
you can upload it to your microcontroller.
Turn the shaft of the rotation sensor slowly. The LED’s brightness should change as you turn it.
DFRobot 09. Light Regulator 05
Code Analysis
Here we have used the map function. The map function is used to map a value within a range to one within another range. Let’s examine it a
little closer:
The function maps a value between fromLow and fromHigh to one between toLow and toHigh.
One advantage of map function is that the lower limit of both ranges can be a number greater than their upper limits:
The value included in the map function can also be a negative number:
y = map (x, 1, 50, 50, -100);
The code above is used to map a value (0-1023) read from the analog pin to one used on the PWM pin (0-255).
Lesson 10
Open Sesame
[Link]
10
DFRobot 10. Open Sesame! 01
Open Sesame!
In this session we are going to use servos to make a door move. The door can’t be opened easily – you will have to complete a
challenge before it does!
The challenge will be explained below…
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1
Digital Tilt Sensor
Micro Servo
x1
TowerPro SG50 Servo
D
DFRobot 10. Open Sesame! 02
Connections
Micro Servo
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
TX 13 12 11 10 9 8 7 6 5 4 3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
BT/APC UNO v3.0[R3]
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
Be sure that your power, ground and signal connections are correct or you risk damaging your components.
When you have connected the components and checked your connections, connect the microcontroller to your PC with the
USB cable so you can upload a program.
DFRobot 10. Open Sesame! 03
Code Input
#include <Servo.h>
int sensorPin = 3; // Digital Vibration Sensor-Digital 3
Servo myservo;
int pos = 0;
void setup() {
[Link](9600);
pinMode(sensorPin, INPUT);
[Link](9); // Servo– Digital 9
}
void loop() {
int sensorState = digitalRead(sensorPin); //Read state of the digital vibration sensor
[Link](sensorState);
if (!sensorState) { //If the state is 0, The servo moves 2° more but does not exceed 180°
pos = pos + 2;
if (pos >= 180) {
pos = 180;
}
[Link](pos); //Write in rotation angle of the Servo
[Link](pos); // Print rotation angle in serial port
delay(100);
} else { // Else, the servo moves 2° less but not less than 0°.
pos = pos - 2;
if (pos <= 0) {
pos = 0;
}
[Link](pos);
[Link](pos);
delay(100);
}
delay(1);
}
You will be able to find it in the Arduino IDE menu bar: Sketch > Import Library > Servo
We cannot use the functions in the library directly. We need to create a servo object in code. It would serve as a medium to connect our code with the
library.
Servo myservo;
Once the servo object is declared, you can use functions from the library with the following method:
[Link]();
We use the function attach() to define which pin the servo should be controlled by.
attach(pin);
The attach () function has one parameter – pin , which refers to one of the digital pins (we do not recommend using digital 0 or 1 due to TX and RX
interference). Here we have chosen digital pin 9.
[Link](9);
[Link]
11
DFRobot 11. Pandora's Box 01
Pandora's Box
An analog ambient light sensor is used to detect the intensity of light outside the box. When the value reaches the limit for the night, the servo will
open the box and an inner-led will glow brighter.
Parts Needed:
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
x1 x1
DIGITAL (PWM~) D
L VCC
GND
5V
SCL
SDA
GND
TX
13 12 *11*10*9 8 7 *6 *5 4 *3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
UNO v3.0[R3] BT/APC
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
ICSP MOSI TXD
VCC
OUT
POWER ANALOG IN
3.3V
RESET
IOREF
A
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
x1 x1
Digital piranha LED-R
Micro Servo
TowerPro SG50 Servo Digital Piranha LED-R
D
Analog Ambient
x1
Analog Ambient Light Sensor V2
A
Light Sensor V2
DFRobot 11. Pandora's Box 02
Connections:
Micro Servo D
I2C
~11
~9
~3
~6
~5
~10
2
8
4
12
13
TX 1
GND
RX 0
AREF
DIGITAL (PWM~) D
VCC
L
GND
5V
SCL
SDA
GND
Analog Ambient
TX 13 12 11 10
Light Sensor V2
9 8 7 6 5 4 3 2 TX RX
DFRduino
SERVO_PWR
RX ON
L *PWM
BT/APC UNO v3.0[R3]
I/O Expansion
SD GND
[Link]
5V
Shield V7
5V
GND DTR
MOSI TXD
A
ICSP
RUN SS RXD
SCK
PROG MISO
3V3 5V
PWR_IN RST 0 1 2 3 4 5
GND
VINGND
VCC
OUT
3.3V
POWER ANALOG IN
RESET
A
IOREF
3V3
VIN
A0
A1
A2
A3
A4
A5
5V
Be sure that your power, ground and signal corrections are correct or you risk damaging your components.
When you have connected the modules and checked your connections, connect the microcontroller to your PC with the USB cable so you can
upload a program.
DFRobot 11. Pandora's Box 03
Code Input
#include <Servo.h>
Servo myservo;
int LED = 3; // Set the LED light to be digital pin 3
int val = 0; // val stores analog ambient light sensor’s value
int pos = 0;
int light = 0;
void setup() {
pinMode(LED, OUTPUT); // LED is set to be in the output mode
[Link](9600); // Baud rate of the serial port is set to be 9600
[Link](9); // attach the servo to digital pin 9
[Link](0); // initial angle is 0
}
void loop() {
val = analogRead(0); // read the analog ambient light sensor’s value
[Link](val); // Check sensor value in serial port
if (val < 40) { // when val is less than the pre-set value, increase the angle
pos = pos + 2;
if (pos >= 90) { // The angle should not be more than 90
pos = 90;
}
[Link](pos); // write angle of the servo
delay(100);
light = map(pos, 0, 90, 0, 255); // As the angle expands, the LED becomes brighter
analogWrite(LED, light); // write the brightness value
} else {
pos = pos - 2; // decrease 2°
if (pos <= 0) {
pos = 0; // until 0°
}
[Link](pos); // write angle of the servo
delay(100);
light = map(pos, 0, 90, 0, 255); // As the angle shrinks, the LED light becomes darker
analogWrite(LED, light); // write the brightness value
}
}
DFRobot 11. Pandora's Box 04
When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your microcontroller.
Attach the servo on to the hinge of the box so that the arm is able to open it. The ambient light sensor needs to be placed outside the box to detect the
ambient lighting. The LED is placed inside the box.
If you have questions about the functions in the sample code, please refer to the previous sessions for details.
DFRobot 16. Digital Dice 07
Be creative and have fun playing with your DFRduino and modules. If
you need more new and exciting modules, you can get them from the
DFRobot Store:
[Link]
If you would like to share your creations with us or have any
questions or comments about these tutorials, log in to the DFRobot
Maker community forum and tell us all about it!
[Link]
Good Luck!