# Embedded System: Arduino Project
:::info
## Outline
[TOC]
:::
## Arduino Official Website
- Download Arduino IDE: https://www.arduino.cc/en/software
- Builtin: https://docs.arduino.cc/language-reference/
- Libraries: https://docs.arduino.cc/libraries/
## Design Flow
1. Decide which type of sensors you need.
2. Learn how to use the sensor.
- Understand the function of each pin on the sensor.
3. Design the circuit for your project; you will probably need a breadboard.
- Ensure power (may be 3.3V or 5V), ground (GND), and signal connections are correct to avoid damaging components.
4. Learn the sensor's API and how to use it.
- Read the sensor's library documentation to know the functions and methods available for interacting with it.
- Some of the libraries are set in `Tools` $\rightarrow$ `Manage Libraries`
5. Code the program, using `Serial.println()` for debugging.
6. Test the system and refine the design
| | Usage | 用途 | |
|:---------- |:----------------------------:|:--------------:|:-------------------------:|
| HLF8574T | LCD | 液晶顯示器 | |
| DHT11 | Humidity, Temperature Sensor | 溫溼度感測器 | |
| DS1302 | Real Time Clock Module | 時間模組 | |
| W104 | Simple Sound Sensor | 聲音偵測模組 | |
| HW-504 | Joystick | 搖桿 | |
| HC-SR04 | Ultrasonic Sensor | 超音波測距模組 | |
| | Water Sensor | 水位測量模組 | |
| ULN2003 AN | Stepper Motor | 步進馬達 | Often use with 28BYJ-48 |
| 28BYJ-48 | Stepper Motor | 步進馬達 | Often use with ULN2003 AN |
| SG90 | Servo Motor | 伺服馬達 | |
| RFID-RC522 | RFID System | 無線射頻辨識 | |
| 5011AS | 7 Segment Display | 7 段顯示器 | |
| 3461BS-1 | 4-Digit 7 Segment Display | 4 位 7 段顯示器 | |
| 1588BS | 8x8 dot matrix | 8x8 點陣 | |
| 74HC595N | 8-bit Shift Register | 移位暫存器 | |
| | Buzzer | 蜂鳴器 | Active, Passive Buzzer |
## Examples
- Ultrasonic Sensor
- Measures distance using sound waves.
- Any project that needs to know the distance can be extended from this example.
- Buzzer
- Active Buzzer:
- Can be used in alert systems, and so on.
- Passive Buzzer:
- Can be used to play a melody.
- Understand the API; it won’t be too difficult.
- RFID System
- You can build a `door access control system` by reading or writing to the card.
- Other Sensors
- Search for the purpose of the sensor using its ID.
## Ultrasonic sensor (HC-SR04)

```c=
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
/* sending a sound wave */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
/*
* If we know when does the sound wave bounce back
* it is possible to know the distance
*/
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
```
> `Tools` -> `Serial Monitor` to see the output
## Buzzers
- Active Buzzer
- Often used in alerts and produce simple sounds.
- Passive Buzzer
- Although it requires specifying a frequency, it allows you to create any melody, offering more flexibility despite being more complex.
```c=
/* Active Buzzer */
void setup() {
pinMode(7,OUTPUT);
}
void loop() {
digitalWrite(7,HIGH); /* turn on the buzzer */
delay(1000);
digitalWrite(7,LOW); /* turn off the buzzer */
delay(2000);
}
```
```c=
/* Passive Buzzer */
#include "pitches.h"
const int buzzer = 8;
// these constants are defined in "pitches.h"
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
for (int i = 0; i < 8; i++) {
// to calculate the note duration, take one second divided by the note type.
int noteDuration = 1000 / noteDurations[i];
/*
* pin number: 8 (any pin number you want)
* sound frequency: melody[i]
* duration: noteDuration
*/
tone(buzzer, melody[i], noteDuration);
// the note's duration + 30% seems to work well:
delay(noteDuration * 1.30);
// stop the tone playing:
noTone(buzzer);
}
}
void loop() {
}
```
## RFID RC522
- Additional library (`Tools` $\rightarrow$ `Manage Libraries`)

```cpp=
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10 /* SDA Pin */
MFRC522 mfrc522; /* declare a MFRC522 object */
void setup() {
Serial.begin(9600);
SPI.begin(); /* init SPI, communication protocal */
mfrc522.PCD_Init(SS_PIN, RST_PIN); /* init MFRC522 */
Serial.print(F("Reader "));
Serial.print(F(": "));
mfrc522.PCD_DumpVersionToSerial(); /* show the version of MFRC522 module */
}
void loop() {
/* if the card is new, then show its UID and type */
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); /* output with your format */
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));
mfrc522.PICC_HaltA();
}
}
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
```