You are on page 1of 16

TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP.

HỒ CHÍ MINH
 ---  --- 

BÀI BÁO CÁO


Chủ đề: ứng dụng của arduino uno
Môn: Thực tập điện

A.STRUCTURE OF ARDUINO UNO.


A.CẤU TẠO ARDUINO UNO.

What's on the board?


There are many varieties of Arduino boards that can be used for different purposes. Some boards look a bit different , but most Arduinos have the majority of these components
in common:
Có gì trên bo mạch?
Có nhiều loại bo arduino, chúng có thể được sử dụng cho những mục đích khác nhau. Một số bo có thể hơi khác, nhưng hầu hết các arduino đều có chung các cầu tạo:

Power (USB / Barrel Jack)


Nguồn(USB/Giắc thùng)
Every Arduino board needs a way to be connected to a power source. The Arduino UNO can be powered from a USB cable coming from your computer or a wall power supply
(like this) that is terminated in a barrel jack. In the picture above the USB connection is labeled (1) and the barrel jack is labeled (2).
Mỗi bo arduino đều cần có một cách kết nối nguồn năng lượng.Arduino Uno được cung cấp năng lượng từ cáp USB kết nối với máy tính hoặc nguồn điện tường (giống thế)
thông qua giắc thùng. Ở hình bên trên kết nối USB kí hiệu (1), và giắc cắm kí hiệu (2).

The USB connection is also how you will load code onto your Arduino board. More on how to program with Arduino can be found in  Installing and Programming
Arduino tutorial.
Kết nối USB cũng là cách bạn đưa code vào mạch arduino của bạn. Hơn nữa bạn có thể tìm hiểu thêm về cách lập trình với Arduino trong hướng dẫn Cài đặt và lập trình
Arduino.

NOTE: Do NOT use a power supply greater than 20 Volts as you will overpower (and thereby destroy) your Arduino. The recommended voltage for most Arduino models is
between 6 and 12 Volts.

Lưu ý: Không sử dụng nguồn cung cấp quá 20V như thế bạn sẽ làm vượt quá công suất của arduino(và bị hỏng). Điện áp được yêu cầu cho hầu hết các loại arduino là từ
6V đến 12 V.

Pins (5V, 3.3V, GND, Analog, Digital, PWM, AREF)


The pins on your Arduino are the places where you connect wires to construct a circuit (probably in conjuction with a breadboard and some wire). They usually have black
plastic ‘headers’ that allow you to just plug a wire right into the board. The Arduino has several different kinds of pins, each of which is labeled on the board and used for
different functions.
Các chân trên Arduino của bạn là những nơi mà bạn kết nối dây để xây dựng một mạch (có lẽ trong kết hợp với breadboard và một số dây).Nó thường có các ‘chân cắm’
cho phép bạn cắm dây ngay vào bo. Có nhiều loại chân cắm khác nhau, mỗi loại đều được ghi tên trên bo và được sử dụng cho các chức năng khác nhau.

GND (3): Short for ‘Ground’. There are several GND pins on the Arduino, any of which can be used to ground your circuit.
GND (3): Viết tắt của ‘Ground’. Có một số chân GND trên Arduino, bất kỳ trong số đó có thể được sử dụng để nối đất cho mạch của bạn.

5V (4) & 3.3V (5): As you might guess, the 5V pin supplies 5 volts of power, and the 3.3V pin supplies 3.3 volts of power. Most of the simple components used with the Arduino
run happily off of 5 or 3.3 volts.
5V (4) & 3.3V (5): Như bạn có thể đoán, chân 5V cung cấp điện áp 5 vôn , và chân 3.3V cung cấp điện áp 3,3vôn. Hầu hết các thành phần đơn giản được sử dụng với Arduino
đều sử dụng điện áp 5 hoặc 3,3 volt.

Analog (6): The area of pins under the ‘Analog In’ label (A0 through A5 on the UNO) are Analog In pins. These pins can read the signal from an analog sensor (like
a temperature sensor) and convert it into a digital value that we can read.
Chân tín hiệu tương tự (6): các chân dưới nhãn 'Analog In' (A0 đến A5 trên UNO) là các chân Analog In. Các chân này có thể đọc tín hiệu từ một cảm biến tương tự (như
cảm biến nhiệt độ) và chuyển đổi nó thành một giá trị số mà chúng ta có thể đọc.

Digital (7): Across from the analog pins are the digital pins (0 through 13 on the UNO). These pins can be used for both digital input (like telling if a button is pushed) and digital
output (like powering an LED).
Kỹ thuật số (7): Qua các chân tương tự là các chân kỹ thuật số (0 đến 13 trên UNO). Các chân này có thể được sử dụng cho cả đầu vào kỹ thuật số (như tín hiệu khi một
nút được nhấn) và đầu ra kỹ thuật số (như cấp nguồn cho đèn LED).

PWM (8): You may have noticed the tilde (~) next to some of the digital pins (3, 5, 6, 9, 10, and 11 on the UNO). These pins act as normal digital pins, but can also be used for
something called Pulse-Width Modulation (PWM). We have a tutorial on PWM, but for now, think of these pins as being able to simulate analog output (like fading an LED in
and out).
PWM (8): Bạn có thể nhận thấy dấu ngã (~) bên cạnh một số chân số (3, 5, 6, 9, 10 và 11 trên UNO). Các chân này hoạt động như các chân số thông thường, nhưng cũng
có thể được sử dụng cho một cái gọi là Pulse-Width Modulation (PWM). Chúng tôi có một hướng dẫn về PWM, nhưng bây giờ, hãy nghĩ đến những chân này có thể mô phỏng
đầu ra tương tự (như điều khiển độ sáng của một LED).

AREF (9): Stands for Analog Reference. Most of the time you can leave this pin alone. It is sometimes used to set an external reference voltage (between 0 and 5 Volts) as the
upper limit for the analog input pins.
AREF (9): Là viết tắt của Analog Reference . Hầu hết thời gian bạn không cần sử dụng chân này. Nó đôi khi được sử dụng để thiết lập một điện áp tham chiếu bên ngoài
(giữa 0 và 5 Volts) như giới hạn trên cho các chân đầu vào tương tự.

Reset Button
Just like the original Nintendo, the Arduino has a reset button (10). Pushing it will temporarily connect the reset pin to ground and restart any code that is loaded on the Arduino.
This can be very useful if your code doesn’t repeat, but you want to test it multiple times. Unlike the original Nintendo however, blowing on the Arduino doesn’t usually fix any
problems.
Nút khởi động:
Giống như bản gốc của Nintendo, Arduino có một nút nhấn khởi động(10).Việc nhấn vào nó sẽ tạm thời sẽ kết nối pin đặt lại vào đất và khởi động bất kì code nào được tải
trên arduino.Điều đó có thể hữu ích nếu code của bạn không lặp lại,nhưng bạn muốn thử nhiều lần. Tuy nhiên không giống như bản gốc Nintendo ,việc nhấn reset trên arduino
thường không khắc phục bất kì vấn đề nào.

Power LED Indicator


Just beneath and to the right of the word “UNO” on your circuit board, there’s a tiny LED next to the word ‘ON’  (11). This LED should light up whenever you plug your Arduino
into a power source. If this light doesn’t turn on, there’s a good chance something is wrong. Time to re-check your circuit!

Đèn LED chỉ báo nguồn


Ngay bên dưới và bên phải của từ “UNO” trên bảng mạch của bạn, có một đèn LED nhỏ bên cạnh từ ‘ON’ (11). Đèn LED này sẽ sáng lên bất cứ khi nào bạn cắm Arduino
vào nguồn điện. Nếu ánh sáng này không bật, thì có khả năng xảy ra lỗi. Đã đến lúc kiểm tra lại mạch của bạn!

TX RX LEDs
TX is short for transmit, RX is short for receive. These markings appear quite a bit in electronics to indicate the pins responsible for  serial communication. In our case, there are
two places on the Arduino UNO where TX and RX appear – once by digital pins 0 and 1, and a second time next to the TX and RX indicator LEDs  (12). These LEDs will give us
some nice visual indications whenever our

TX là viết tắt của transmit (truyền) , RX là viết tắt của receive(nhận). Những dấu hiệu này xuất hiện khá nhiều trong các thiết bị điện tử để chỉ ra các chân chịu trách nhiệm
về giao tiếp nối tiếp.Trên bo arduino uno, có hai vị trí nơi TX và RX xuất hiện - một là ở các chân số 0 và 1, và hai là bên cạnh đèn LED chỉ báo TX và RX (12). Những đèn LED
này sẽ cung cấp cho chúng ta một số chỉ dẫn trực quan đẹp mắt bất cứ khi nào.

B.SIMPLE ARDUINO AUDIO PLAYER AND AMPLIFIER WITH LM386


B.MÁY NGHE NHẠC VÀ BỘ KHUẾCH ĐẠI ÂM THANH ARDUINO ĐƠN GIẢN VỚI LM386

ARDUINO

Adding sounds or music to our project will always make it looks cool and sounds much more attractive. Especially if you are using an Arduino and you have lots of pins
free, you can easily add sound effects to your project by just investing in an extra SD card module and a normal speaker. In this article I will show you how easy it is to Play
music/add sound effects using your Arduino Board.  Thanks to the Arduino community who have developed some libraries to build this in a fast and easy way. We have
also used IC LM386 here for amplification and noise cancelation purpose.
Việc thêm âm thanh hoặc âm nhạc vào dự án của chúng tôi sẽ luôn làm cho âm thanh trở nên tuyệt vời và hấp dẫn hơn nhiều. Đặc biệt là nếu bạn đang sử dụng Arduino
và bạn có rất nhiều ghim miễn phí, bạn có thể dễ dàng thêm hiệu ứng âm thanh vào dự án của mình bằng cách chỉ đầu tư thêm một mô-đun thẻ SD và loa bình thường. Trong
bài viết này, tôi sẽ chỉ cho bạn cách dễ dàng để chơi nhạc / thêm hiệu ứng âm thanh bằng cách sử dụng Arduino Board của bạn. Nhờ cộng đồng Arduino đã phát triển một số
thư viện để xây dựng điều này một cách nhanh chóng và dễ dàng. Chúng tôi cũng đã sử dụng IC LM386 ở đây để khuếch đại và mục đích hủy tiếng ồn.

Hardware Required:

1. Arduino UNO
2. SD Card Reader module
3. SD card
4. LM386 Audio Amplifier
5. 10uf Capacitor (2 Nos)
6. 100uf Capacitor (2 Nos)
7. 1K,10K Resistor
8. Push buttons (2 Nos)
9. Breadboard
10. Connecting Wires
Yêu cầu phần cứng:
1. Arduino UNO
2. Mô-đun đầu đọc thẻ SD
3. thẻ SD
4. Bộ khuếch đại âm thanh LM386
5. Tụ điện 10uf (2 Nos)
6. Tụ điện 100uf (2 Nos)
7. 1K, điện trở 10K
8. Các nút ấn (2 Nos)
9. Breadboard
10. Kết nối dây điện

Getting ready with your WAV audio files:

For playing sounds from SD Card using Arduino, we need audio files in .wav format because Arduino Board can play an audio file in a specific format that is wav format. To
make an arduino mp3 player, there are a lot of mp3 shields are available which you can use with arduino.  Or else to play mp3 files in arduino, there are websites which you can
be used to convert any audio file on your computer into that specific WAV file.
Chuẩn bị sẵn sàng với các tệp âm thanh WAV của bạn:
Để phát âm thanh từ Thẻ SD sử dụng Arduino, chúng tôi cần tệp âm thanh ở định dạng .wav vì Bảng Arduino có thể phát tệp âm thanh ở định dạng cụ thể có định dạng
wav. Để thực hiện một máy nghe nhạc mp3 arduino, có rất nhiều lá chắn mp3 có sẵn mà bạn có thể sử dụng với Arduino. Hoặc người nào khác để chơi các tập tin mp3 trong
Arduino, có những trang web mà bạn có thể được sử dụng để chuyển đổi bất kỳ tập tin âm thanh trên máy tính của bạn vào tập tin WAV cụ thể

So to convert any audio file into wav format, follow the below steps:
Vì vậy, để chuyển đổi bất kỳ tập tin âm thanh thành định dạng wav, hãy làm theo các bước dưới đây:

Step 1: Click on “Online Wav Converter” to enter into the website.


Bước 1: Nhấp vào “Chuyển đổi Wav trực tuyến” để truy cập vào trang web.

Step 2: Arduino can play a wav file in the following format. You can toy around with the settings later, but these settings were experiment to be the best in quality.
Bước 2: Arduino có thể phát tệp wav theo định dạng sau. Bạn có thể đồ chơi xung quanh với các cài đặt sau, nhưng các cài đặt này đã được thử nghiệm để có chất lượng
tốt nhất.

Bit Resolution 8 Bit


Sampling Rate 16000 Hz
Audio Channel Mono
PCM format PCM unsigned 8-bit

Step 3: In the website click on “choose file” and select the file you want to convert. Then feed in the above settings. Once done it should look something like this in the below
image
Bước 3: Trong trang web nhấp vào “chọn tệp” và chọn tệp bạn muốn chuyển đổi. Sau đó, nguồn cấp dữ liệu trong cài đặt ở trên. Sau khi thực hiện nó sẽ giống như thế này
trong hình dưới đây

Step 4:  Now, click on “Convert File” and your Audio file will be converter to .Wav file format. It will also be downloaded once the conversion is done.
Bước 4: Bây giờ, bấm vào "Chuyển đổi tập tin" và tập tin âm thanh của bạn sẽ được chuyển đổi sang định dạng tệp .Wav. Nó cũng sẽ được tải xuống khi quá trình chuyển
đổi hoàn tất.

Step 5: Finally format your SD card and save your .wav audio file into it. Make sure you format it before you add this file. Also remember the name of your audio file.
Similarly you can select any of your four audios and save them with names 1, 2, 3 and 4(Names should not be changed). I have converted four songs and have saved them as
1.wav, 2.wav, 3.wav and 4.wav like shown below.
Bước 5: Cuối cùng định dạng thẻ SD của bạn và lưu tệp âm thanh .wav của bạn vào đó. Hãy chắc chắn rằng bạn định dạng nó trước khi bạn thêm tập tin này. Cũng nhớ
tên của tập tin âm thanh của bạn. Tương tự như vậy bạn có thể chọn bất kỳ của bốn âm thanh của bạn và lưu chúng với tên 1, 2, 3 và 4 (Tên không nên thay đổi). Tôi đã
chuyển đổi bốn bài hát và đã lưu chúng dưới dạng 1.wav, 2.wav, 3.wav và 4.wav như được hiển thị bên dưới.
Circuit and Hardware:

Circuit Diagram for this Arduino Audio File Player is simple. The complete circuit diagram is shown in the Image below.

Mạch và phần cứng:


Sơ đồ mạch cho Arduino File Audio Player này rất đơn giản. Sơ đồ mạch hoàn chỉnh được hiển thị trong Hình bên dưới.

As we know our audio files are saved into the SD card, hence we interface a SD card reader module with our Arduino. The Arduino and SD card communicate using the SPI
communication protocol. Hence the Module is interfaced with the SPI pins of the Arduino as shown above in the diagram. It is further listed in the table below.
Như chúng ta biết các tập tin âm thanh của chúng tôi được lưu vào thẻ SD, do đó chúng tôi giao diện một mô-đun đầu đọc thẻ SD với Arduino của chúng tôi. Thẻ Arduino
và SD giao tiếp bằng giao thức truyền thông SPI. Do đó Module được giao tiếp với các chân SPI của Arduino như được hiển thị ở trên trong biểu đồ. Nó được liệt kê thêm
trong bảng dưới đây.

Arduino SD card module


+5V Vcc
Gnd Gnd
Pin 12 MISO (Master In Slave out)
Pin 11 MOSI (Master Out Slave In)
Pin 13 SCK (Synchronous Clock)
Pin 4 CS (Chip Select)

Now the Arduino will be able to read the music file from the SD card and play it on the pin number 9. But the audio signals produced by the Arduino on pin 9 will not be
audible much. Hence we amplify it by using the LM386 Low voltage Audio amplifier IC.
Bây giờ Arduino sẽ có thể đọc các tập tin âm nhạc từ thẻ SD và chơi nó trên số pin 9. Nhưng các tín hiệu âm thanh được sản xuất bởi Arduino trên pin 9 sẽ không thể
nghe được nhiều. Do đó chúng tôi khuếch đại nó bằng cách sử dụng IC khuếch đại âm thanh điện áp thấp LM386.

The amplifier shown above is designed for a Gain of 200 and the Vdd (pin 6) is powered by the 5V pin of the Arduino. If you want to increase/decrease the sound you can
increase/decrease the voltage provided to this pin. It can withstand a maximum of 15V. Learn more about this 200 gain amplification configuration for LM386 here.
We also have two push buttons connected to the pin 2 and 3 of the Arduino. These switches are used to play the next track of the song and play/pause the music respectively. I
have used these buttons just to demonstrate its abilities; you can play the song whenever required. Check the Demo Video at the end.
Các bộ khuếch đại được hiển thị ở trên được thiết kế cho một tăng 200 và VDD (pin 6) được cung cấp bởi chân 5V của Arduino. Nếu bạn muốn tăng / giảm âm thanh, bạn
có thể tăng / giảm điện áp được cung cấp cho pin này. Nó có thể chịu được tối đa 15V. Tìm hiểu thêm về cấu hình khuếch đại tăng 200 này cho LM386 tại đây.
Chúng tôi cũng có hai nút nhấn kết nối với pin 2 và 3 của Arduino. Các công tắc này được sử dụng để phát bài hát tiếp theo của bài hát và phát / tạm dừng nhạc tương ứng.
Tôi đã sử dụng các nút này chỉ để chứng minh khả năng của nó; bạn có thể chơi bài hát bất cứ khi nào cần thiết. Kiểm tra Video giới thiệu ở cuối.

You can assemble this circuit completely over a Breadboard as shown in the picture below

Bạn có thể lắp ráp mạch này hoàn toàn trên một Breadboard như thể hiện trong hình dưới đây
Programming your Arduino:

Lập trình Arduino của bạn:

Once we are ready with the Hardware and the SD card, we are just one step away playing those songs. Insert the card into your SD card module and follow the steps
below.
Một khi chúng tôi đã sẵn sàng với phần cứng và thẻ SD, chúng tôi chỉ là một bước đi chơi những bài hát. Lắp thẻ vào mô-đun thẻ SD của bạn và thực hiện theo các bước
bên dưới.

Step 1: As said earlier we will be using a library to make this project work. The link for the library is given below. Click on it and select “Clone or download” and choose
download as ZIP.
 TMRpcm library

Bước 1: Như đã nói ở trên, chúng ta sẽ sử dụng thư viện để làm cho dự án này hoạt động. Liên kết cho thư viện được đưa ra dưới đây. Click vào nó và chọn "Clone hoặc
download" và chọn tải xuống dưới dạng ZIP.
 Thư viện TMRpcm

Step 2: Add this Zip file into your Arduino IDE by selecting Sketch->Include Library -> Add .ZIP Library as shown below and select the ZIP file that we just downloaded.

Bước 2: Thêm tệp Zip này vào IDE Arduino của bạn bằng cách chọn Sketch-> Include Library -> Thêm Thư viện .ZIP như hình dưới đây và chọn tệp ZIP mà chúng tôi vừa
tải xuống.
Step 3: The complete program of the arduino music player project is given at the end of this article, simply copy it and paste it in the Arduino Program. Now, click on Upload
and get ready to play your audio files.

Bước 3: Chương trình hoàn chỉnh của dự án trình phát nhạc arduino được đưa ra ở cuối bài viết này, chỉ cần sao chép và dán nó vào Chương trình Arduino. Bây giờ, hãy
nhấp vào Tải lên và sẵn sàng phát các tệp âm thanh của bạn.

The program is self explanatory since they have the comment lines. But, I have also explained the ability of the TMRpcm library below.

Chương trình tự giải thích vì chúng có dòng chú thích. Nhưng, tôi cũng đã giải thích khả năng của thư viện TMRpcm bên dưới.
Playing an audio file:
You can play any audio that is stored in Wav format inside the SD card module by using the line below.

Phát tệp âm thanh:


Bạn có thể phát bất kỳ âm thanh nào được lưu trữ ở định dạng Wav bên trong mô-đun thẻ SD bằng cách sử dụng dòng bên dưới.
music.play("3.wav");
//object name.play (“FileName.wav”);

You can use this line at places where you want to trigger the Audio
Pause an audio File:
To pause an Audio file, you can simply call the line below.

Bạn có thể sử dụng dòng này ở những nơi bạn muốn kích hoạt âm thanh
Tạm dừng tệp âm thanh:
Để tạm dừng tệp Âm thanh, bạn có thể chỉ cần gọi dòng bên dưới.
music.pause();
//objectname.pause();

Forwarding/Rewinding an Audio:
There are not direct ways to forward or rewind an Audio file, but you can use the line below to play a song at a particular time. This can be used to forward/rewind with some
additional programming.
Chuyển tiếp / tua lại âm thanh:
Không có cách trực tiếp để chuyển tiếp hoặc tua lại tệp Âm thanh, nhưng bạn có thể sử dụng dòng bên dưới để phát bài hát tại một thời điểm cụ thể. Điều này có thể được
sử dụng để chuyển tiếp / tua lại với một số chương trình bổ sung.

music.play("2.wav",33); //Plays the song from 33rd second


//objectname.play(“Filename.wav”,time in second);

Setting the quality of the audio:


The library gives us two qualities to play the music, one is to play as normal mode the other to play with 2X oversampling.
Đặt chất lượng âm thanh:
Thư viện cung cấp cho chúng tôi hai phẩm chất để chơi nhạc, một là chơi như chế độ bình thường để chơi với 2X oversampling.
music.quality(0); //Normal Mode
music.quality(1); //2X over sampling mode

Setting the Volume of the audio:


Yes, you can control the volume of the audio through software. You can simply set the volume by using the line below. Higher music volumes tend to affect the quality of the
audio, hence use hardware control when possible.
Đặt âm lượng của âm thanh:
Có, bạn có thể kiểm soát âm lượng của âm thanh thông qua phần mềm. Bạn chỉ có thể đặt âm lượng bằng cách sử dụng dòng bên dưới. Âm lượng nhạc cao hơn có xu
hướng ảnh hưởng đến chất lượng âm thanh, do đó sử dụng điều khiển phần cứng khi có thể.

music.setVolume(5); //Plays the song at volume 5


//objectname.setVolume(Volume level);

Working of this Arduino Music Player:

After programming your Arduino simply press the button connected to pin 2 and your Arduino will play the first song (saved as 1.wav) for you.   Now you can press the button
again to change your track to the next song that is to play 2.wav. Likewise you can navigate to all four songs.
You can also play/Pause the song by pressing the button connected to pin 3. Press it once to pause the song and press it again to play it from where it stopped.   Watch
the video below for complete working (or maybe to relax yourself with some songs).
Làm việc của Arduino Music Player này:
Sau khi lập trình Arduino của bạn chỉ cần nhấn nút kết nối với pin 2 và Arduino của bạn sẽ chơi bài hát đầu tiên (lưu dưới dạng 1.wav) cho bạn. Bây giờ bạn có thể nhấn nút
một lần nữa để thay đổi bài hát của bạn thành bài hát tiếp theo để chơi 2.wav. Tương tự như vậy bạn có thể điều hướng đến tất cả bốn bài hát.
Bạn cũng có thể phát / Tạm dừng bài hát bằng cách nhấn nút kết nối với ghim 3. Nhấn một lần để tạm dừng bài hát và nhấn lại lần nữa để phát bài hát từ nơi nó dừng lại.
Xem video dưới đây để hoàn thành công việc (hoặc có thể tự thư giãn với một số bài hát).

Hope you enjoyed the project. Now it is up to your creativity to use them in your projects. You can make a speaking clock, voice assistant, talking robot, voice alert security
system and much more. Let me know how you are planning to use it through the comment section and also if you have any problems in getting this thing work you can reach
me through the forums or the comment section below.
Hy vọng bạn thích dự án này. Bây giờ tùy thuộc vào sự sáng tạo của bạn để sử dụng chúng trong các dự án của bạn. Bạn có thể tạo đồng hồ nói, trợ lý thoại, rô bốt nói
chuyện, hệ thống bảo mật cảnh báo bằng giọng nói và hơn thế nữa. Hãy cho tôi biết làm thế nào bạn đang có kế hoạch để sử dụng nó thông qua phần bình luận và cũng có
thể nếu bạn có bất kỳ vấn đề trong việc điều này làm việc bạn có thể tiếp cận với tôi thông qua các diễn đàn hoặc phần bình luận dưới đây.

Code:
/*
Arduino Based Music Player
This example shows how to play three songs from SD card by pressing a push button
The circuit:
 * Push Button on pin 2 and 3
 * Audio Out - pin 9
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
created  25 Jun 2017
 by Aswinth Raj
This example code was created for CircuitDigest.com
*/
 
#include "SD.h" //Lib to read SD card
#include "TMRpcm.h" //Lib to play auido
#include "SPI.h" //SPI lib for SD card
#define SD_ChipSelectPin 4 //Chip select is pin number 4
TMRpcm music; //Lib object is named "music"
int song_number=0;
boolean debounce1=true;
boolean debounce2=true;
boolean play_pause;
void setup(){
music.speakerPin = 9; //Auido out on pin 9
Serial.begin(9600); //Serial Com for debugging 
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
pinMode(2, INPUT_PULLUP); //Button 1 with internal pull up to chage track
pinMode(3, INPUT_PULLUP); //Button 2 with internal pull up to play/pause
pinMode(3, INPUT_PULLUP); //Button 2 with internal pull up to fast forward
music.setVolume(5);    //   0 to 7. Set volume level
music.quality(1);        //  Set 1 for 2x oversampling Set 0 for normal
//music.volume(0);        //   1(up) or 0(down) to control volume
//music.play("filename",30); plays a file starting at 30 seconds into the track    
}
void loop()

  
  if (digitalRead(2)==LOW  && debounce1 == true) //Button 1 Pressed
 {
  song_number++;
  if (song_number==5)
  {song_number=1;}
  debounce1=false;
  Serial.println("KEY PRESSED");
  Serial.print("song_number=");
  Serial.println(song_number);
if (song_number ==1)
  {music.play("1.wav",10);} //Play song 1 from 10th second
if (song_number ==2)
  {music.play("2.wav",33);} //Play song 2 from 33rd second
if (song_number ==3)
  {music.play("3.wav");} //Play song 3 from start
if (song_number ==4)
  {music.play("4.wav",25);} //Play song 4 from 25th second
if (digitalRead(3)==LOW  && debounce2 == true) //Button 2 Pressed
 {
  music.pause();  Serial.println("PLAY / PAUSE");
  debounce2=false;
 }
if (digitalRead(2)==HIGH) //Avoid debounce
  debounce1=true;
if (digitalRead(3)==HIGH)//Avoid debounce
  debounce2=true;
}
}

C.WIFI CONTROLLED ROBOT USING ARDUINO


C.ROBOT ĐIỀU KHIỂN BẰNG WIFI SỬ DỤNG ARDUINO

There are many types of Robots, from the simple ones like a Toy car to the advanced ones like industrial Robots

Có nhiều loại robot,từ một thứ đơn giản như một chiếc xe đồ chơi đến một robot công nghiệp nâng cao .

This time we are going to make a Wi-Fi controlled Robot using Arduino and Blynk App. This Arduino based Robot can be controlled wirelessly using any Wi-Fi enabled Android
smart phone.

Lần này chúng ta sẽ làm một robot được điều khiển bằng wifi sử dụng arduino và ứng dụng Blynk. Arduino dựa trên việc robot có thể được điều khiển không dây sử dụng
wifi kết nối điện thoại andriod.

For demonstration of Wi-Fi Controlled Robot, we have used an Android Mobile App named “Blynk”. Blynk is a very compatible app with Arduino, to make IoT based project.
This App can be downloaded from the Google Play Store, and can be easily configured.
Để trình diễn Robot điều khiển Wi-Fi, chúng tôi đã sử dụng Ứng dụng dành cho thiết bị di động Android có tên “Blynk”. Blynk là một ứng dụng rất tương thích với Arduino,
để làm cho dự án dựa trên IoT. Ứng dụng này có thể được tải xuống từ Cửa hàng Google Play và có thể dễ dàng được định cấu hình.

Steps for configuring Blynk App:

Các bước để chỉnh cấu hình ứng dụng Blynk:

1. First download it from Google Play Store and install it in Android mobile phone. (Trước tiên tải nó từ của hàng google play và cải đặt trên điện thoại android)

2. After this, it is required to create an account. You may use your current Gmail account.

(Sau đó ,nó yêu cầu tạo tài khoản .Bạn có thể sử dụng tài khoản gmail hiện tại)

3. Now select Arduino Board and give a name for your project.

(Bây giờ chọn mạch arduino và đặt tên cho dự án của bạn)
 
4. Note down the Auth Token Code or simply mail it to your Email Account and then copy and paste in Arduino sketch (Program Code).
Lưu ý Mã xác thực mã thông báo hoặc chỉ cần gửi nó vào tài khoản email của bạn và sau đó sao chép và dán trong bản phác thảo Arduino (Mã chương trình)

5.  Enter this Auth Token Code in Arduino sketch.


(Nhập Mã thông báo xác thực này vào bản phác thảo Arduino.)

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "caa17a11c0124d4083d0eaa995f45917";

6. Then click on create button in Blynk app.


(Sau đó nháy vào tạo nút button trên ứng dụng Blynk)

7.  Now Select the Joystick Widget, Click on Joystick, Configure the Joystick and hit the back button.
Bây giờ chọn Joystick Widget, Click vào Joystick, cấu hình Joystick và nhấn nút quay lại.
   

8. After it press Play button at the right top of screen.

Sau đó nhấn nút Phát ở đầu màn hình bên phải

Required Components:(Các thành phần yêu cầu)

 Arduino UNO
 ESP8266 Wi-Fi  Module
 USB Cable
 Connecting wires
 L293D
 DC Motors
 Batteries
 10K POT   (optional)
 Robot chassis plus wheel
 Roller caster
 Android Mobile phone
 Blynk App

Circuit Explanation:(Lý giải mạch điện)

Circuit Diagram of Wi-Fi controlled robot is given below. We mainly need a Arduino and ESP8266 Wi-Fi module. ESP8266’s Vcc and GND pins are directly connected to
3.3V and GND of Arduino and CH_PD is also connected with 3.3V. Tx and Rx pins of ESP8266 are directly connected to pin 2 and 3 of Arduino. Software Serial Library is used
to allow serial communication on pin 2 and 3 of Arduino. We have already covered the Interfacing of ESP8266 Wi-Fi module to Arduino in detail.
Sơ đồ mạch của robot điều khiển Wi-Fi được đưa ra dưới đây. Chúng ta chủ yếu cần một mô-đun Wi-Fi Arduino và ESP8266. Chân Vcc và GND của ESP8266 được kết nối
trực tiếp với 3.3V và GND của Arduino và CH_PD cũng được kết nối với 3.3V. Các chân Tx và Rx của ESP8266 được kết nối trực tiếp với chân 2 và 3 của Arduino. Phần mềm
thư viện nối tiếp được sử dụng để cho phép giao tiếp nối tiếp trên pin 2 và 3 của Arduino. Chúng tôi đã giới thiệu chi tiết về mô đun Interfacing ESP8266 Wi-Fi cho Arduino.

A L293D Motor Driver IC is used for driving DC motors. Input pins of motor driver IC is directly connected to pin 8, 9, 10 and 11 of Arduino. And DC motors are connected at
its output pins. Here we have used 9 Volt battery for driving the Circuit and DC motors.

Một IC điều khiển động cơ L293D được sử dụng để điều khiển động cơ DC. Chân đầu vào của IC điều khiển động cơ được kết nối trực tiếp với pin 8, 9, 10 và 11 của
Arduino. Và động cơ DC được kết nối tại chân đầu ra của nó. Ở đây chúng tôi đã sử dụng pin 9 Volt để điều khiển động cơ Mạch và Động cơ DC.

Working Explanation:(Nguyên lí làm việc)

Working of the Wi-Fi controlled Robot is very easy, we just need to Drag or Slide the Joystick in the direction, where we want to move the Robot. Like if we want to move the
Robot in Forward direction then we need to Drag the Joystick ‘circle’ in Forward direction. Likewise we can move the Robot in Left, Right and Backward direction by Dragging
the joystick in respective direction. Now as soon as we release the Joystick, it will come back to centre and Robot stops.

Làm việc của Robot điều khiển Wi-Fi rất dễ dàng, chúng tôi chỉ cần kéo hoặc trượt cần điều khiển theo hướng, nơi chúng tôi muốn di chuyển Robot. Giống như nếu chúng
ta muốn di chuyển Robot theo hướng Chuyển tiếp thì chúng ta cần kéo "Vòng tròn" của Joystick theo hướng Chuyển tiếp. Tương tự, chúng ta có thể di chuyển Robot theo
hướng Trái, Phải và Lùi bằng cách kéo cần điều khiển theo hướng tương ứng. Bây giờ ngay sau khi chúng ta nhả Joystick, nó sẽ trở lại trung tâm và Robot dừng lại.

Blynk App sends values from Two Axis Joystick to Arduino, through Wi-Fi medium. Arduino receive the values, compare them with predefined values and move the Robot
accordingly in that direction.
Blynk App gửi các giá trị từ Two Axis Joystick đến Arduino, thông qua phương tiện Wi-Fi. Arduino nhận được các giá trị, so sánh chúng với các giá trị được xác định trước
và di chuyển Robot theo hướng đó.

Programming Explanation:(Giải thích chương trình)

Program is almost readymade available in Arduino IDE. We just need to download Blynk Library for Arduino. And after making some modifications, user can make own Wi-Fi
controlled robot.

Chương trình gần như là sẵn có trong Arduino IDE. Chúng tôi chỉ cần tải Blynk Library cho Arduino. Và sau khi thực hiện một số sửa đổi, người dùng có thể tạo rô bốt
được điều khiển bằng Wi-Fi riêng.

First we have included all the needed libraries to run this code in Arduino IDE, and then entered Auth Token, from the Blynk app, in the  auth string. Here we are connecting Wi-
Fi serial pin with Software Serial of Arduino. Selected pin 2 as RX and 3 as TX.

Đầu tiên chúng tôi đã khai báo tất cả các thư viện cần thiết để chạy mã này trong Arduino IDE, và sau đó nhập Mã xác thực, từ ứng dụng Blynk, trong chuỗi auth. Ở đây
chúng tôi đang kết nối pin nối tiếp Wi-Fi với Phần mềm nối tiếp Arduino. Đã chọn pin 2 là RX và 3 là TX.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space

#include <ESP8266_SoftSer.h>

#include <BlynkSimpleShieldEsp8266_SoftSer.h>

// Set ESP8266 Serial object

#include <SoftwareSerial.h>

SoftwareSerial EspSerial(2, 3); // RX, TX


ESP8266 wifi(EspSerial);

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "caa17a11c0124d4083d0eaa995f45917";

Then we defined output pins(8,9,10,11) for motors and wrote some direction functions to move the Robot in particular direction:  void forward(), void backward(),void
right() and void left()  

After this, in setup function we initialise all the required devices, like giving direction to motor pins, begin serial communication, providing Wi-Fi username and password.

Sau đó, chúng tôi xác định chân đầu ra (8,9,10,11) cho động cơ và đã viết một số chức năng hướng để di chuyển Robot theo hướng cụ thể: void forward (), void backward
(), void right () và void left () Sau này, trong chức năng thiết lập, chúng tôi khởi tạo tất cả các thiết bị cần thiết, như đưa ra hướng đến chân động cơ, bắt đầu truyền thông nối
tiếp, cung cấp tên người dùng và mật khẩu Wi-Fi.

void setup()

// Set console baud rate

Serial.begin(9600);

delay(10);

// Set ESP8266 baud rate

// 9600 is recommended for Software Serial

EspSerial.begin(9600);

delay(10);

Blynk.begin(auth, wifi, "username", "password"); // wifi username and password

pinMode(m11, OUTPUT);

pinMode(m12, OUTPUT);

pinMode(m21, OUTPUT);

pinMode(m22, OUTPUT);

Now we have checked some conditions for controlling the robot. Here we have selected  virtual pin 1 (V1) for taking input from Blynk App to control the robot. As we have
used marge option in app so we will get x and y axis values at same pin.

Bây giờ chúng tôi đã kiểm tra một số điều kiện để điều khiển robot. Ở đây chúng ta đã chọn pin ảo 1 (V1) để lấy đầu vào từ ứng dụng Blynk để điều khiển robot. Vì chúng ta
đã sử dụng tùy chọn marge trong ứng dụng nên chúng ta sẽ nhận được các giá trị trục x và y ở cùng một pin.

BLYNK_WRITE(V1)

int x = param[0].asInt();

int y = param[1].asInt();

if(y>220)
forward();

else if(y<35)

backward();

else if(x>220)

right();

else if(x<35)

left();

else

Stop();

At last we need to run blynk function in loop, to run the system.

Cuối cùng, chúng ta cần chạy hàm blynk trong vòng lặp, để chạy hệ thống.

void loop()

Blynk.run();

Code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_SoftSer.h>
#include <BlynkSimpleShieldEsp8266_SoftSer.h>

// Set ESP8266 Serial object


#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

ESP8266 wifi(EspSerial);

// You should get Auth Token in the Blynk App.


// Go to the Project Settings (nut icon).
char auth[] = "caa17a11c0124d4083d0eaa995f45917";

#define m11 8
#define m12 9
#define m21 10
#define m22 11

void forward()
{
  digitalWrite(m11, HIGH);
  digitalWrite(m12,LOW);
  digitalWrite(m21,HIGH);
  digitalWrite(m22,LOW);
}

void backward()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,HIGH);
  digitalWrite(m21,LOW);
  digitalWrite(m22,HIGH);
}

void right()
{
  digitalWrite(m11, HIGH);
  digitalWrite(m12,LOW);
  digitalWrite(m21,LOW);
  digitalWrite(m22,LOW);
}
void left()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,LOW);
  digitalWrite(m21,HIGH);
  digitalWrite(m22,LOW);
}

void Stop()
{
  digitalWrite(m11, LOW);
  digitalWrite(m12,LOW);
  digitalWrite(m21,LOW);
  digitalWrite(m22,LOW);
}

void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  // 9600 is recommended for Software Serial
  EspSerial.begin(9600);
  delay(10);

  Blynk.begin(auth, wifi, "username", "password");  // wifi username and password


  pinMode(m11, OUTPUT);
  pinMode(m12, OUTPUT);
  pinMode(m21, OUTPUT);
  pinMode(m22, OUTPUT);
}

BLYNK_WRITE(V1) 
{
  int x = param[0].asInt();
  int y = param[1].asInt();

  // Do something with x and y


/*  Serial.print("X = ");
  Serial.print(x);
  Serial.print("; Y = ");
  Serial.println(y);*/
  if(y>220)
  forward();
  else if(y<35)
  backward();
  else if(x>220)
  right();
  else if(x<35)
  left();
  else
  Stop();
}

void loop()
{
  Blynk.run();
}

You might also like