SunFounder - Arduino

119
Preface About SunFounder SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strive to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you build your own project. If you have interest in open source or making something cool, welcome to join us! Visit www.sunfounder.com for more! About RFID Kit This RFID kit is suitable for the SunFounder Uno, SunFounder Mega 2560, SunFounder Duemilanove, and SunFounder Nano. All codes found in the user guide are compatible with these boards. Our SunFounder board is fully compatible with Arduino. This kit includes some simple and complicated experiments, which is closely related with life. Through these experiments, you can clearly know the principles behind some common phenomenon and apply them well. For example, password lock and entrance card system. You can also conduct these experiments by yourself to strengthen what you have learnt. In this book, we will show you circuits with both realistic illustrations and schematic diagrams. You can go to our official website www.sunfounder.com to download related materials (code, Fritzing figures, etc.) by clicking LEARN -> Get Tutorials -> Arduino -> RFID Kit V2.0 for Arduino . Free Support If you have any TECHNICAL questions, add a topic under FORUM section on our website and we'll reply as soon as possible. For NON-TECH questions like order and shipment issues, please send an email to [email protected]. You're also welcomed to share your projects on FORUM. Rept 3.0

Transcript of SunFounder - Arduino

PrefaceAbout SunFounderSunFounder is a technology company focused on Raspberry Pi and Arduino open sourcecommunity development. Committed to the promotion of open source culture, we strive tobring the fun of electronics making to people all around the world and enable everyone tobe a maker. Our products include learning kits, development boards, robots, sensormodules and development tools. In addition to high quality products, SunFounder alsooffers video tutorials to help you build your own project. If you have interest in open sourceor making something cool, welcome to join us! Visit www.sunfounder.com for more!

About RFID KitThis RFID kit is suitable for the SunFounder Uno, SunFounder Mega 2560, SunFounderDuemilanove, and SunFounder Nano. All codes found in the user guide are compatiblewith these boards.

Our SunFounder board is fully compatible with Arduino.

This kit includes some simple and complicated experiments, which is closely related with life.Through these experiments, you can clearly know the principles behind some commonphenomenon and apply them well. For example, password lock and entrance card system.You can also conduct these experiments by yourself to strengthen what you have learnt.

In this book, we will show you circuits with both realistic illustrations and schematic diagrams.You can go to our official website www.sunfounder.com to download related materials(code, Fritzing figures, etc.) by clicking LEARN -> Get Tutorials -> Arduino -> RFID Kit V2.0 forArduino.

Free Support

If you have any TECHNICAL questions, add a topic under FORUM section on ourwebsite and we'll reply as soon as possible.

For NON-TECH questions like order and shipment issues, please send an email [email protected]. You're also welcomed to share your projects on FORUM.

Rept 3.0

ContentsComponents List.............................................................................................................................................. 1

Get Started......................................................................................................................................................10

Lesson 1 Controlling LED by Button.......................................................................................................... 16

Lesson 2 Controlling LED by PWM.............................................................................................................21

Lesson 3 Interactive LED Flowing Lights...................................................................................................26

Lesson 4 I2C LCD1602...................................................................................................................................28

Lesson 5 Buzzer...............................................................................................................................................31

Lesson 6 Tilt Switch.........................................................................................................................................34

Lesson 7 Building a Quiz Buzzer System...................................................................................................37

Lesson 8 Serial Monitor.................................................................................................................................40

Lesson 9 Photoresistor...................................................................................................................................44

Lesson 10 Controlling Buzzer by Photoresistor....................................................................................... 47

Lesson 11 Flame Alarm................................................................................................................................ 50

Lesson 12 Making a Voltmeter.................................................................................................................. 53

Lesson 13 Sound Sensor...............................................................................................................................55

Lesson 14 LM35 Temperature Sensor....................................................................................................... 57

Lesson 15 Water Level Detection............................................................................................................. 59

Lesson 16 7-Segment Display.....................................................................................................................61

Lesson 17 Stopwatch – 4-Digit 7-Segment Display.............................................................................. 66

Lesson 18 Dot-matrix Display......................................................................................................................69

Lesson 19 Rainbow LED................................................................................................................................74

Lesson 20 Driving 7-Segment Display by 74HC595...............................................................................76

Lesson 21 Vibration Switch..........................................................................................................................80

Lesson 22 Real-time Clock Module..........................................................................................................83

Lesson 23 Humiture Detection...................................................................................................................86

Lesson 24 Relay..............................................................................................................................................90

Lesson 25 Stepper Motor.............................................................................................................................93

Lesson 26 Servo.............................................................................................................................................. 98

Lesson 27 Joystick PS2................................................................................................................................102

Lesson 28 Infrared-Receiver..................................................................................................................... 105

Lesson 29 RFID Entrance Guard System................................................................................................107

Lesson 30 Password Lock.......................................................................................................................... 112

1

1

Components ListNo. Name Qty. Component

1 I2C LCD1602 1

2 RTC-DS1302 1

3 Sound Sensor 1

49V Battery

Buckle1

2

5 Breadboard 1

6 LED (red) 8

7 LED (green) 8

8 LED (yellow) 8

9 Resistor (220Ω) 8(red, red, black, black, brown)

10 Resistor (1KΩ) 4

(brown, black, black, brown, brown)

11 Resistor (10KΩ) 4(brown, black, black, red, brown)

3

12Potentiometer

(50K)1

13 Active Buzzer 1

14Passive Buzzer

Module1

15 74HC595 2

16 IR Receiver 1

4

17 LM35 2

18 Flame Sensor 1

19 Photoresistor 2

20 Remote Control 1

21Four-digit

Segment Display 1

5

22 Stepper Motor 1

23Stepper Motor

Driver1

24 Servo 1

25 Relay Module 1

267-segment

Display2

6

27 Joystick PS2 1

28 Button 5

29 Tilt Switch 1

30 RFID Module 1

31 RFID Key Tag 1

7

32 Humiture Sensor 1

33Water Level

Measurement

Module

1

344*4 Keypad

Module1

358*8 Dot-matrix

Module1

36 Vibration Switch 1

8

37 RGB LED 1

385-Pin Anti-reverse

Cable2

394-Pin Anti-reverse

Cable2

403-Pin Anti-reverse

Cable2

41Dupont Wire (F

to F)10

9

42Jumper Wire

(M to M)30

43 USB Cable 1

44Dupont wire

(M to F)10

10

Get StartedNote:

Before starting your own project, you must download the file RFID Kit V2.0 for Arduino.zip onour official website by visiting LEARN -> Get Tutorials -> RFID Kit V2.0 for Arduino and unzip it.

About this part, for your better learning, we've provided a video tutorial on our websiteunder VIDEO and you can check it out for more.

Arduino

Description

Arduino is an open source platform with simple software and hardware. You can pick it upin short time even if you are a beginner. It provides an integrated developmentenvironment (IDE) for code compiling, compatible with multiple control boards. So you canjust download the Arduino IDE, upload the sketches (i.e. the code files) to the board, andthen you can see relative experimental phenomena. For more information, refer tohttp://www.arduino.cc.

SunFounder Arduino Board

11

Now, Sunfounder has four Arduino boards namely Sunfounder Uno R3 Board, Mega 2560Board, Nano Board, and Mars Board. The Uno Board and the Mars Board share the samekind of pins at the same position. And the pins of the first half part on the Mega 2560 arethe same as those on the Uno. The difference lies in that on the Mega 2560 board, the I2Cpin (SDA) and pin SCL don't correspond to pin A4 and A5 but to pin 20 and 21. On theother hand, the Nano and the Uno also share the same kind of pins at the same position.Though the Nano is in a smaller size, making it applicable for projects.

The following is the parameters of the four boards:

Board

Parameters

Uno R3 Mars Nano Mega 2560

Microcontroller ATmega328P ATmega328P ATmega328P ATmega2560

USB connection Type B Type C Mini-B Type B

USB-to-Serial Chip MEGA16U2 FT232R PL2303 MEGA16U2

Power jack? Y Y N Y

12

OperatingVoltage

5V 5V 5V 5V

Input Voltage 7-12V 7-12V 7-12V 7-12V

Digital I/O Pins14 (of which 6provide PWM

output)

14 (of which6 provide

PWM output)

14 (of which 6provide PWM

output)

54 (of which15 provide

PWM output)

Analog Input Pins 6 6 8 16

DC Current perI/O Pin

20 mA 20mA 20 mA 20 mA

13

Install Arduino IDE

The code in this kit is written based on Arduino, so you need to install the IDE first. Skip it ifyou have done this.

Now go to the arduino.cc website and click DOWNLOAD. On the page, check the softwarelist on the right side under Download the Arduino Software.

Find the one that suits your operation system and click to download. There are two versionsof Arduino for Windows: Installer or ZIP file. You're recommended to download the former.Just download the package, and run the executable file to start installation. It willdownload the driver needed to run Arduino IDE. After downloading, follow the prompts toinstall. For the details of installing steps, you can refer to the guide on Learning->GettingStarted with Arduino, scroll down and see Install the Arduino Software.

After installing, you will see Arduino icon on your desk. Double click to open it.

Add Libraries

Since in some code, the libraries needed are not included in Arduino, so you need to addthem before compiling.

14

Click Sketch -> Included Library -> Add .ZIP Library

Go to the folder of the zip file you've downloaded. Go to Library->Dht and click Open.

When you see "Library added to your libraries .Check 'include library' menu" appears, itmeans the library has been added successfully. You may add the rest libraries needed inthe similar way.

15

Plug in the BoardConnect the control board to your computer with a USB cable. If you use Uno, Mega2560or Mars as the control board, the system will automatically install the driver when you plug itin. After a while, a prompt will show up at the bottom right corner telling you which port theboard locates at. If the control board is Nano, then you need to install the driver by yourself.For more details, you can refer toWIKI on our website www.sunfounder.com.

For more details about Arduino IDE, go to Learning->Getting started->Foundation on thearduino.cc and click Arduino Software (IDE) on the pagehttp://www.arduino.cc/en/Guide/Environment

If your sketch fails upload, on the same page click Troubleshootinghttp://www.arduino.cc/en/Guide/Troubleshooting.

Notes:

- If your computer is running on the Windows XP system, the new version IDE will prompterrors when running the code. You are recommended to download the Arduino 1.0.5or Arduino 1.0.6. Or you can also upgrade your Window system.

- All the experiments in this kit are done with SunFounder Uno R3 board, but they are alsocompatible with SunFounder Mega 2560, SunFounder Mars, SunFounder Nano and allofficial Arduino Boards. All the code included in this kit works with these boards.

So what does COMPATIBLE mean here? It means you can use any of the three boardsto do the same experiment with the same wiring. Simply put, if the wire is connected toPin 12 of Uno in the user manual, likewise, you can connect it to Pin 12 on any otherofficial Arduino boards you are using. Then open the corresponding sketch and uploadthem.

Now let's start to make the experiments!

16

Lesson 1 Controlling LED by Button

IntroductionIn this experiment, we will learn how to turn an LED on/off by using an I/O port and a button.The "I/O port" refers to the INPUT and OUTPUT port. Here the INPUT port of the SunFounderUno board is used to read the output of an external device. Since the board itself has anLED (connected to Pin 13), so you can use this LED to do this experiment for convenience.

Components- 1 * SunFounder Uno board- 1 * USB cable- 1 * Button- 1 * Resistor (10kΩ)- Jumper wires- 1 * Breadboard

PrincipleButtonButtons are a common component used to control electronic devices. They are usuallyused as switches to connect or break circuits. Although buttons come in a variety of sizesand shapes, the one used here is a 6mm mini-button as shown in the following pictures.

Pin 1 is connected to pin 2 and pin 3 to pin 4. So you just need to connect either of pin 1and pin 2 to pin 3 or pin 4.

The following is the internal structure of a button. Since the pin 1 is connected to pin2, andpin 3 to pin 4, the symbol on the right below is usually used to represent a button.

17

When the button is pressed, the 4 pins are connected, thus closing the circuit.

Generally, the button can be connected directly to the LED in a circuit to turn on or off theLED, which is comparatively simple. However, sometimes the LED will brighten automaticallywithout any button pressed, which is caused by various kinds of external interference. Inorder to avoid this interference, a pull-down resistor is used – usually connect a 1K–10KΩresistor between the button and GND. It can be connected to GND to consume theinterference when the button is off.

This circuit connection is widely used in numerous circuits and electronic devices. We mayuse the button to control a circuit later in many experiments (in or outside this kit maybe), soyou might get its principle, which is very simple, and application at the beginning of yourstudy.

Experimental ProceduresStep 1: Build the circuit

18

The schematic diagram

Step 2: Open the code file

In the folder RFID_Kit_v2.0_for_Arduino you just downloaded and unzipped, you can see thefolder for the corresponding lesson which includes two folders: code (the sketch) andcircuit (Fritzing file). Go to the code folder and find the .ino file (.ino is the format of thesketch). Then double click to open it.

Step 3: Select the correct Board

19

and Port

Step 4: Upload

Click the Upload icon to upload the code to the board. The sketches can be uploaded tothe board when there is nothing wrong with them. During the upload, the TX LED and theRX LED will be alternately flickering. It means the board is sending signal to the computerand then receives the signal from the computer. After upload is completed, the two LEDswill go out.

20

If "Done uploading" appears at the bottom of the window, it means the sketch has beensuccessfully uploaded. And if you see the pin 13(L) LED starts to flicker, it means the codehas been successfully run.

Note: The experimental steps for the subsequent lessons are the same. Here we just takeone lesson for example. You can modify the code as you like, but pay attention to save itas another one for reference.

Now, press the button, and the LED on the SunFounder Uno board will light up.

Tips: For any TECHNICAL questions, add a topic under FORUM section on our websitewww.sunfounder.com and we'll reply as soon as possible.

21

Lesson 2 Controlling LED by PWM

IntroductionIn this lesson, let’s try something a little easier – gradually changing the luminance of an LEDthrough programming. Since the pulsing light looks like breathing, we give it a magicalname - breathing LED. We’ll accomplish this effect with pulse width modulation (PWM).

Components- 1 * SunFounder Uno board- 1 * Breadboard- Jumper wires- 1 * LED- 1 * Resistor (220Ω)- 1 * USB cable

PrinciplePWMPulse width modulation, or PWM, is a technique for getting analog results with digital means.Digital control is used to create a square wave, a signal switched between on and off. Thison-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) bychanging the portion of the time the signal spends on versus the time that the signal spendsoff. The duration of "on time" is called the pulse width. To get varying analog values, youchange, or modulate, that pulse width. If you repeat this on-off pattern fast enough with anLED for example, the result is as if the signal is a steady voltage between 0 and 5Vcontrolling the brightness of the LED. (See the PWM description on the official website ofArduino).

22

We can see from the top oscillogram that the amplitude of the DC voltage output is 5V.However, the actual voltage output is only 3.75V through PWM because the high level onlytakes up 75% of the total voltage within a period.

Here is an introduction to the three basic parameters of PWM:

1. Duty cycle describes the proportion of "on" time to the regular interval or period of time.

2. Period describes the reciprocal of pulses in one second.

23

3. The voltage amplitude here is 0V–5V.

24

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

25

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Here you should see the LED gets brighter and brighter, and then slowly dimmer, just likebreathing.

26

Lesson 3 Interactive LED Flowing Lights

IntroductionIn this lesson, you will conduct a simple yet interesting experiment – using LEDs to createflowing LED lights. As the name implies, these flowing lights are made up of eight LEDs in arow which successively light up and dim one after another, just like flowing water.

Components- 1* SunFounder Uno board- 1 * Breadboard- 8 * LED- 8 * Resistor (220Ω)- 1 * Potentiometer- 1 * USB cable- Several jumper wires

Experimental PrincipleThe principle is quite simple, that is, to turn eight LEDs on in turn. And then change the timeinterval of LED on and off by adjusting the potentiometer.

Experimental ProceduresStep 1: Build the circuit

27

The schematic diagram

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Here you should see eight LEDs light up one by one in turn. Adjust the potentiometer, andyou will find the time interval of LED lighting up changes.

28

Lesson 4 I2C LCD1602

IntroductionAs we all know, though LCD and some other displays greatly enrich the man-machineinteraction, they share a common weakness. When they are connected to a controller,multiple IOs will be occupied of the controller which has no so many outer ports. Also itrestricts other functions of the controller. Therefore, LCD1602 with an I2C bus is developedto solve the problem.

I2C bus is a type of serial bus invented by PHLIPS. It is a high performance serial bus whichhas bus ruling and high or low speed device synchronization function required by multiple-host system. The blue potentiometer on the I2C LCD1602 (see the figure below) is used toadjust the backlight for better display. I²C uses only two bidirectional open-drain lines,Serial Data Line (SDA) and Serial Clock Line (SCL), pulled up with resistors. Typical voltagesused are +5 V or +3.3 V although systems with other voltages are permitted.

Components- 1 * SunFounder Uno board- 1 * I2C LCD1602 module- 1 * USB cable- Several jump wires

Experimental PrincipleIn this experiment, we will let I2C LCD1602 display “SunFounder" and “hello, world" byprogramming.

29

Experimental ProceduresStep 1: Build the circuitThe connection between the I2C LCD1602 and the SunFounder Uno board:

I2C LCD1602 SunFounder Uno

GND GND

VCC 5V

SDA A4

SCL A5

Note: The wiring of I2C LCD1602 is the same through the following lessons.

If you use a Mega2560 board, the wiring is different then.

30

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

You should now see your I2C LCD1602 display the flowing characters: "SunFounder" and"hello, world".

31

Lesson 5 BuzzerIntroductionA buzzer is a great tool in your experiments whenever you want to make some sounds. .

Components- 1 * SunFounder Uno board- 1 * Breadboard- 1 * USB data cable- 1 * Buzzer (Active)- Jumper wires

Experimental PrincipleAs a type of electronic buzzer with an integrated structure, buzzers, which are supplied byDC power, are widely used in computers, printers, photocopiers, alarms, electronic toys,automotive electronic devices, telephones, timers and other electronic products for voicedevices. Buzzers can be categorized as active and passive ones (see the following picture).Turn the pins of two buzzers face up, and the one with a green circuit board is a passivebuzzer, while the other enclosed with a black tape is an active one.

The difference between an active buzzer and a passive buzzer:

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. Buta passive buzzer does not have such source, so it will not tweet if DC signals are used;instead, you need to use square waves whose frequency is between 2K and 5K to drive it.The active buzzer is often more expensive than the passive one because of multiple built-inoscillating circuits.

In this experiment, we use an active buzzer.

32

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

33

Now, you should hear the buzzer beep.

34

Lesson 6 Tilt Switch

IntroductionThe tilt switch used here is a ball one with a metal ball inside. It is used to detect small angleof inclination.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Tilt switch- Several jumper wires

Experimental PrincipleThe principle is very simple. When the switch is tilted in a certain angle, the ball inside rollsdown and touches the two contacts connected to the pins outside, thus triggering circuits.Otherwise the ball will stay away from the contacts, thus breaking the circuits.

35

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

36

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, tilt the switch, and the LED attached to pin 13 on SunFounder Uno board will light up.

37

Lesson 7 Building a Quiz Buzzer System

IntroductionIn quiz shows, especially entertainment activities (e.g. competitive answering activities),organizers often apply a buzzer system in order to accurately, fairly and visually determinethe seat number of a responder.

Now the system can illustrate the accuracy and equity of the judgment by data, whichimproves the entertainment. At the same time, it is more fair and just. In this lesson, we willuse some buttons, buzzers, and LEDs to make a quiz buzzer system.

Components- 1* SunFounder Uno board- 1 * USB data cable- 4 * Button- 4 * LED- 4 * Resistor (220Ω)- 1 * Active Buzzer- Several jumper wires- 1 * Breadboard

Experimental PrincipleButton 1, 2 and 3 are answer buttons, and button 4 is the reset button. If button 1 is pressedfirst, the buzzer will beep, the corresponding LED will light up and all the other LEDs will goout. If you want to start another round, press button 4 to reset.

38

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

39

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, first press button 4 to start. If you press button 1 first, you will see the corresponding LEDlight up and the buzzer will beep. Then press button 4 again to reset before you press otherbuttons.

40

Lesson 8 Serial Monitor

IntroductionIn this experiment, you will learn how to turn on or off LEDs through a computer and theSerial Monitor. Serial Monitor is used for communication between the Uno board and acomputer or other devices. It is a built-in software in the Arduino environment and you canclick the button on the upper right corner to open it. You can send and receive data viathe serial port on the Uno board and control the board by input from the keyboard.

In this experiment, since we use colored LEDs as loads, you can enter a color among red,green, and blue on Serial Monitor in the IDE. The corresponding LED on the SunFounder Unoboard will then light up.

Components- 1 * SunFounder Uno board- 1 * Breadboard- 3 * LED- 3 * Resistor (220Ω)- Jumper wires- 1 * USB cable

PrincipleHere, the Serial Monitor serves as a transfer station for communication between yourcomputer and the SunFounder Uno board. First, the computer transfers data to the SerialMonitor, and then the data is read by the SunFounder Uno board. Finally, the Uno willperform related operations.

41

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

42

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, click the Serial Monitor button at the upper right corner in the IDE. Then the SerialMonitor window will pop up as shown below.

With this window, you can not only send data from your computer to the SunFounder Unoboard, but also receive data from the board and display it on the screen. When you openthe window, it will display "Please input any color of LED:". You can input a color here. If youenter red, green, or blue, click Send, then the corresponding LED on the breadboard willlight up. However, if you enter any color other than these three, no LEDs will be on.

43

For example, if you input red, you will see the red LED light up.

44

Lesson 9 Photoresistor

IntroductionA photoresistor or photocell is a light-controlled variable resistor. The resistance of aphotoresistor decreases with increasing incident light intensity; in other words, itexhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits,and light- and darkness-activated switching circuits.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Photoresistor- 1 * Resistor (10KΩ)- 8 * LED- 8 * Resistor (220Ω)- Jumper wires-1 * Breadboard

Experimental PrincipleThe resistance of a photoresistor changes with incident light intensity. If the light intensitygets higher, the resistance decreases; if it gets lower, the resistance increases.

In this experiment, we will use eight LEDs to show the light intensity. The higher the lightintensity is, the more LEDs will light up. When the light intensity is high enough, all the LEDswill be on. When there is no light, all the LEDs will go out.

45

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

46

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, shine some light on the photoresistor, and you will see several LEDs light up. Shinemore light and you will see more LEDs light up. When you place it in a dark environment, allthe LEDs will go out.

ExplorationIn addition to the above experiment, you can replace the photoresistor with a microphoneand use LEDs to indicate sound intensity. The higher the sound intensity gets, the more LEDswill light up. You can realize this effect by yourself. Try now!

47

Lesson 10 Controlling Buzzer by Photoresistor

IntroductionSince you have learnt how to use a photoresistor, in this lesson, you will learn how to controla buzzer to make it beep in different frequencies by the photoresistor.

Compoment- 1* SunFounder Uno board- 1 * USB data cable- 1 * Photoresistor- 1 * Buzzer (Active)- 1 * Resistor (10KΩ)- Several jumper wires-1 * Breadboard

Experiment PrincipleWhen you shine some light on the photoresistor, if the incident light gets stronger, theresistance of the photoresistor will decrease; if the incident light becomes weaker, theresistance will increase.

In this experiment, the output of the photoresistor is sent to pin A0 on the SunFounder Unoboard and then processed by the ADC on the board to output a digital signal. We use thisdigital signal as the parameter of the delay() function in the sketch to make the buzzerbeep.

When the incident light is strong, the output value gets greater, thus the buzzer will beepslowly; when incident light is weak, the output value is smaller, thus the buzzer will beepsharply.

48

Experiment ProceduresStep 1: Build the circuit

The schematic diagram

49

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, place the photoresistor in a dark environment, and the buzzer will beep sharply; shinea flashlight onto the photoresistor, and the beeping will slow down.

50

Lesson 11 Flame Alarm

IntroductionA flame sensor module performs detection by capturing infrared wavelengths from flame.It can be used to detect and warn of flames. In this experiment, we use the sensor to makea flame alarm.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Flame sensor module- 1 * Passive buzzer module- 1 * 4-Pin anti-reverse cable- 1 * 3-Pin anti-reverse cable- 1 * Jump wire (F to F)

Experimental PrincipleThere are several types of flame sensors. In this experiment, we will use a far-infrared flamesensor. It can detect infrared light with a wavelength ranging from 700nm to 1000nm. A far-infrared flame probe converts the strength changes of the external infrared light intocurrent changes. And then it converts analog quantities into digital ones.

In this experiment, connect pin D0 to the digital port 8 of the SunFounder board. Then whenthe flame sensor detects flame signals, the buzzer beeps and the corresponding LED lightsup. When it detects no flame signals, the buzzer stops and the LED goes out.

51

The schematic diagram of the module is as follows:

Experimental ProceduresStep 1: Build the circuitThe wiring between the flame sensor and SunFounder Uno board:

Flame Sensor SunFounder Uno

D0 8

A0 A0

VCC 5V

GND GND

The wiring between the passive buzzer and SunFounder Uno board:

Passive Buzzer SunFounder Uno

SIG 7

VCC 5V

GND GND

52

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, ignite a lighter near the flame sensor. Then the buzzer will beep, and the LED on theflame sensor module and that attached to pin 13 of the SunFounder board will light up.

53

Lesson 12 Making a Voltmeter

IntroductionIn this lesson, we will use a potentiometer and an I2C LCD1602 to make a voltmeter.

Components- 1* SunFounder Uno board- 1 * USB data cable- 1 * Potentiometer (50KΩ)- 1 * I2C LCD1602- Several jumper wires- 1 * Breadboard

Experimental PrincipleIn this experiment, a potentiometer is used to divide voltage. Since the SunFounder Uno

board can only read digital signals, but what the sliding end of the potentiometer outputs

are analog signals, we need to convert these analog signals into digital ones with an

Analog-to-Digital Convertor (ADC). Fortunately, the SunFounder Uno board itself comes

with a 10-bit ADC which we can use to implement this conversion. Then display this digital

output voltage on the I2C LCD1602.

Experimental ProceduresStep 1: Build the circuit

54

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, adjust the potentiometer and you will see the voltage displayed on the I2C LCD1602varies accordingly.

55

Lesson 13 Sound SensorIntroductionSound Sensor is a component that receives sound waves and converts them into electricalsignals. It detects the sound intensity in the ambient environment like a microphone does.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Sound Sensor module- 1 * 3-Pin anti-reverse cable

Experimental PrincipleThe sensor has a capacitive electret microphone which is sensitive to sound. Sound wavescause the thin film of the electret to vibrate and then the capacitance changes, thuscausing the corresponding voltage change. Since the change is extremely weak, it needsto be amplified. LM358 here is a power amplifier of 100 times gain. Connect the SIG outputpin of the sound sensor to A0 of the SunFounder Uno board. Then you can see the value ofsound intensity on Serial Monitor, or display the wave forms on an oscilloscope if you haveconnected one.

The schematic diagram of the module is as shown below:

56

Experimental ProceduresStep 1: Build the circuit

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now you can see the value of sound intensity on Serial Monitor. When the volume reachesto a certain value, the LED attached to pin 13 on the SunFounder Uno board will light up.

57

Lesson 14 LM35 Temperature Sensor

IntroductionLM35 is a temperature sensor produced by National Semiconductor. It has very highoperational accuracy and a wide operating range. With small size, low cost and reliability,LM35 is widely applied in engineering. Since it uses internal compensation, the output canbegin with 0℃ . LM35 has many different packages. Under the normal temperature, theLM35 requires no additional calibration to reach the accuracy of plus or minus 1/4℃. Thepower supply mode can be classified as single power source and positive-and-negativedouble power supply. Its pins are as shown below. Under the positive-and-negative dualpower supply mode, it can measure a negative temperature. Under the single powersupply mode and 25℃ , the quiescent current is about 50μA and it has a wide operationvoltage range - between 4 to 20V, thus saving electricity.

Components- 1* SunFounder Uno board- 1 * Breadboard- 1 * USB data cable- 1 * LM35 Temperature Sensor- 1 * I2C LCD1602- Several jumper wires

Experimental PrincipleThe output voltage of the LM35 is proportional to Celsius temperature. When placed in 0℃

ambient temperature, it will output 0V. The output voltage will increase 10mV each timethe temperature increases by 1℃. The calculation formula is as follows:

58

Experimental ProceduresStep 1: Build the circuit

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you can see the current temperature displayed on the I2C LCD1602.

59

Lesson 15 Water Level Detection

IntroductionIn this lesson, we will use a water level sensor to measure the depth of the water anddisplay the result on I2C LCD1602.

Components- 1 * SunFounder Uno board- 1 * Breadboard- 1 * USB data cable- 1 * Water Level Sensor- 1 * I2C LCD1602- Several jumper wires

Experimental PrincipleWater level sensor is a module that can sense the depth of water, whose core part is anamplification circuit composed of a transistor and several comb-shape PCB cables. Whenplaced in water, the comb-shape cable will change its resistance with the depth of thewater and convert the depth signal into an electrical signal. With an ADC on theSunFounder Uno board dealing with that signal, we can know the water depth changes.

Experimental ProceduresStep 1: Build the circuit

60

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, if you immerse the sensor in water, you can see the depth to which the sensor issoaked displayed on the I2C LCD1602.

61

Lesson 16 7-Segment Display

IntroductionIn this lesson, we will learn how to use a 7-segment display which consists of severalcommon cathode LEDs connected in parallel. It is a simple and common component fordisplaying numbers or characters. The code used this time illuminates only one LED per unittime. The high refresh rate – higher than the resolution speed of the naked eye – enables usto see the numbers clearly. The 7-segment display also has the advantage of high anduniform luminance.

Components- 1 * SunFounder Uno board- 1 * 7-segment display (Common Cathode)- 8 * Resistor (220Ω)- 1 * USB cable- Jumper wires- 1 * Breadboard

Experimental PrincipleThe 7-segment display consists of seven LEDs arranged in a rectangular fashion. Each of theseven LEDs is called a segment because, when illuminated, the segment forms part of anumerical digit (both Decimal and Hex) to be displayed. An additional 8th dot LED issometimes used within the same package, thus allowing the indication of a decimal point(DP) when two or more 7-segment displays are connected together to display numbersgreater than nine.

Each of the seven LEDs in the display is given a positional segment with one of itsconnection pins being brought straight out of the rectangular plastic package. These LED

62

pins are labeled from a through to g representing each individual LED. The other LED pinsare connected together and wired to form a common pin.

So by forward biasing the appropriate pins of the LED segments in a particular order, somesegments will be lit and others will stay off allowing the desired character or number to bedisplayed. Therefore it can display each of the ten decimal digits from 0 to 9 on the same7-segment display.

The display’s common pin is generally used to identify the type of 7-segment display. Aseach LED has two connecting pins, the "Anode" and "Cathode", there are two types of LED7-segment display: Common Cathode (CC) and Common Anode (CA).

The difference between the two types, as their names suggest, is that the commoncathode display has all the cathodes of the 7 segments connected directly together whilethe common anode one has all the anodes connected.

The Common Cathode – In the common cathode display, the connected cathodes of theLED segments are joined together to logic "0" or ground. Each individual segment isenergized by the application of a "HIGH" or logic "1" signal via a current limiting resistor sothe individual anode terminal (a-g) is forward biased.

Common Cathode 7-Segment Display

The Common Anode – In the common anode display, the connected anodes of the LEDsegments are joined together to logic "1". Each individual segment is energized by applyinga ground, logic "0" or "LOW" signal via a suitable current limiting resistor to the cathode ofthe particular segment (a-g).

63

Common Anode 7-Segment Display

Experimental ProceduresStep 1: Build the circuit

The wiring between the 7-segment display and the SunFounder Uno board:

7-Segment Display SunFounder Uno

a 7

b 6

c 5

d 11

e 10

f 8

g 9

dp 4

"-" GND

64

The schematic diagram

65

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

You should now see the 7-segment display showing characters circularly from 0 to 9 andthen A to F, back and forth.

66

Lesson 17 Stopwatch – 4-Digit 7-Segment Display

IntroductionIn this lesson, we will use a 4-digit 7-segment display to make a stopwatch.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * 4-Digit 7-Segment Display- Several jumper wires- 1 * Breadboard- 8 * Resister (220Ω)

Experimental PrincipleWhen a 7-segment display is used, if it is a common anode display, connect the anode pinto power source; if it is a common cathode one, connect the cathode pin to GND. When a4-digit 7-segment display is used, the common anode or common cathode pin is to controlthe digit displayed. There is only one digit working. However, based on the principle ofPersistence of Vision, we can see four 7-segment displays all displaying numbers. This isbecause the electronic scanning speed is too fast for us to notice interval.

The schematic diagram of the 4-digit 7-segment display is as shown below:

67

Experimental ProceduresStep 1: Build the circuitThe wiring between the 4-digit 7-segment display and the SunFounder Uno board is asshown below:

4-Digit 7-Segment Display SunFounder Uno

a 2

b 3

c 4

d 5

e 6

f 7

g 8

p 9

D1 13

D2 12

D3 11

D4 10

68

The schematic diagram

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you can see the number increases by one per second on the 4-digit 7-segmentdisplay.

69

Lesson 18 Dot-matrix Display

IntroductionWith low-voltage scanning, LED dot-matrix displays have various advantages such aspower saving, long service life, low cost, high brightness, view of a wide angle, long visualrange, waterproof capability, and numerous specifications. They can meet the needs ofdifferent applications and thus have a broad development prospect. This time, we willconduct an LED dot-matrix experiment to experience its charm firsthand.

Components- 1 * SunFounder Uno board- 1 * 8*8 dot-matrix- 8 * Resistor (220Ω)- 1 * Breadboard- Jumper wires- 2 * 74HC595- 1 * USB cable

PrincipleThe external view of a dot-matrix is as follows:

For pin definition:

Define row and column numbering first (only for a dot-matrix whose model number endswith BS)

70

Pin numbering corresponding to the above rows and columns:

COL 1 2 3 4 5 6 7 8

Pin No. 13 3 4 10 6 11 15 16

ROW 1 2 3 4 5 6 7 8

Pin No. 9 14 8 12 1 7 2 5

The display principle of the 8*8 dot-matrix:

The 8*8 dot-matrix is made up of sixty-four LEDs and each LED is placed at the cross point ofa row and a column. When the electrical level of a certain row is High and the electricallevel of a certain column is Low, then the corresponding LED will light up; if you want to lightthe LED on the first dot, you should set ROW 1 to high level and COL 1 to low level, then theLED on the first dot will light up; if you want to light the LEDs on the first row, you should setROW 1 to high level and COL (1, 2, 3, 4, 5, 6, 7, 8) to low level, then all the LEDs on the firstrow will light up; if you want to light the LEDs on the first column, you should set COL 1 to lowlevel and ROW (1, 2, 3, 4, 5, 6, 7, 8) to high level, then all the LEDs on the first column willlight up.

The principle of 74HC595 has been illustrated previously. One chip is used to control therows of the dot-matrix while the other is to control the columns.

71

The schematic diagram

Experimental ProceduresStep 1: Build the circuitSince the wiring in this experiment is a little complex, let's complete it step by step.

1) Plug the necessary devices and components on the breadboard. Then connect pin16and pin 10 of the two 74HC595 to 5V of the control board, and pin 8 and pin 13 to GND.

72

2) Then connect pin 11 of the two 74HC595, then to pin 12 of Uno. Connect pin 12 of thetwo chips and then to Uno pin 8. Pin 14 of the 74HC595 on the left to pin 9 of that oneon the right, and pin 14 of the 74HC595 on the right side to pin 11 of Uno.

3) The 74HC595 on the left controls rows of the dot matrix. To avoid burning the matrix, weadd a 220Ohm current-limiting resistor to each row. The connection of 74HC595 pinswith dot matrix ones are done in a sequence: Q0-Q7 of the 74HC595 respectively to pin5, 2, 7, 1, 12, 8, 14, and pin 9 (corresponding to row 8 to 1).

73

4) The 74HC595 on the right controls columns of the dot matrix. Pin Q0-Q7 of the 74HC595are connected to pin 13, 3, 4, 10, 6, 11, 15, and 16.

Step 2: Open the code fileStep 3: Select the correct Board and PortStep 4: Upload the sketch to the SunFounder Uno board

The dot-matrix will display 0 to 9 and then A to F circularly, back and forth.

74

Lesson 19 Rainbow LED

IntroductionRGB LED modules can emit various colors of light. Three LEDs of red, green, and blue arepackaged into a transparent or semitransparent plastic shell with four pins led out. Thethree primary colors of red, green, and blue can be mixed and compose all kinds of colorsby brightness, so you can make an RGB LED emit colorful light by controlling the circuit.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * RGB LED module- 1 * 4-Pin anti-reverse cable

Experimental PrincipleIn this experiment, we will use PWM technology to control the brightness of RGB. We'vedescribed the concept in Lesson 2 of this kit. You may check the information, if necessary.

Each of the three color channels: red, green, and blue, has 255 stages of brightness. Whenthe three primary colors are all 0, the LED light is turned off. When the colors are all 255, theLED gets brightest. Here we input any value between 0 and 255 to the three pins of theRGB LED to make it display different colors.

RGB LEDs can be categorized into common anode LED and common cathode LED. In thisexperiment, we use a common cathode RGB LED. See the schematic diagram as below:

75

Experimental ProceduresStep 1: Build the circuit

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

You should now see the 7-segment display cycle from 0 to F.

76

Lesson 20 Driving 7-Segment Display by 74HC595

IntroductionIn this experiment, we will learn how to use 74HC595 to drive a single 7-segment display todisplay numbers.

Generally speaking, there are two methods to drive a 7-segment display. One is to connect8 pins directly to eight ports on the SunFounder Uno board, just as we did previously. Theother is to connect 74HC595 to three ports of the SunFounder Uno board and connect the7-segment display to 74HC595. In this experiment, we will use the latter method. Theadvantage of this method is that you can save five ports, which is very importantconsidering the SunFounder Uno board’s limited ports.

Components- 8 * Resistor (220Ω)- 1 * 74HC595- 1 * SunFounder Uno board- Several Jumper wires (M to M)- 1 * Breadboard- 1 * USB cable- 1 * 7-segment display

Experimental Principle

74HC59574HC595 has an 8-bit shift register and memory with a three-state output function. Its mainfunction is to transform serial data input into parallel data output so that you can save theI/O ports of MCU. 74HC595 is mainly used in multipath LED indication or multi-bit segmentdisplay driving. At the same time, it supports three-state output. When the 13th pin is at highlevel, there will be no output from 74HC595. With a data latching function, 74HC595 doesnot affect the instant output during the shifting process; with a data output function,74HC595 enables you to cascade 74HC595 more conveniently.

77

The pins of 74HC595 and their functions:Q0-Q7: Pins for 8-bit parallel data output, able to control 8 LEDs or the 8 pins of a 7-segmentdisplay directly.Q7’: Series output pin connected to DS pin of the next 74HC595 to connect multiple 595s inseriesMR: Reset pin, effective at low level; here it is directly connected to 5V.SH: Time sequence input of shift register. On the rising edge, the data in shift register movessuccessively one bit, i.e. data in Q1 moves to Q2, and so forth. While on the falling edge,the data in shift register remains unchanged.ST: Time sequence input of memory register. On the rising edge, data in the shift registermoves into memory register.OE: Output enable pin, effective at low level, connected to the ground directly.Ds: Serial data input pinVCC: The power supplyGND: The ground

Here the shiftout() function is applied, which comes with the SunFounder IDE whenprogramming. Simply input a number from 0 to 255 and the memory register can transformit into an 8-bit binary number and output it in parallel. This allows you to easily control the 8pins of the 7-segment display and create any pattern you want.

Experimental ProceduresStep 1: Build the circuit

The wiring between the 7-segment display, 74HC595 and Uno board is as shown below:

7-Segment Display 74HC595 SunFounder Uno

a Q7

b Q6

c Q5

d Q4

e Q3

f Q2

g Q1

DP Q0

78

VCC 5V

DS 11

CE GND

ST 12

SH 8

MR 5V

Q7’ N/C

GND GND

“ - ” GND

79

The schematic diagram

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now you can see the 7-segment display show from 0 to 9 and then A to F, back and forth.

80

Lesson 21 Vibration Switch

IntroductionA vibration switch is a device that recognizes the amplitude of the vibration to which it isexposed and provides some sort of response when this amplitude exceeds apredetermined threshold value. The switch response is typically an electrical contactclosure or contact opening. The electrical contact may be either an electromechanicalrelay or solid-state triac.

Components- 1*SunFounder Uno board- 1*USB data cable- 1* vibration switch module- Jumper wires

Experimental PrincipleThe main principle of vibration switch is that, conductive vibration spring and trigger pin areprecisely placed in switch ontology and bond to curing position through adhesive.Normally, the spring and the trigger pin do not contact. Once shook, the spring will shakeand contact with trigger pin to conduct and generate trigger signals.

With the LED attached to pin 13, connect the vibration switch to digital pin D8. When thevibration switch inducts vibration signals, pin SIG will output low level, and the LED on themodule and the LED attached to pin 13 will light up. The schematic diagram of thevibration switch module is shown as follows.

81

Experimental ProceduresStep 1: Build the circuit

82

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, shake the switch module, and you should see the LED attached to pin 13 on theSunFounder Uno board and the LED on the module light up.

83

Lesson 22 Real-time Clock ModuleIntroductionNowadays there are many popular serial clock circuits such as DS1302, DS1307, PCF8485,etc. They are widely used for simple interface, low cost, and ease of use. In this lesson, wewill use the DS1302 real-time clock (RTC) module to obtain current date and time.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * RTC-DS1302 module- 1 * I2C LCD1602 module- 1 * 5-Pin anti-reverse cable- 1 * 4-Pin anti-reverse cable- 1 * Dupont wire (F to F)

Experimental PrincipleDS1302 is a trickle charging clock chip, launched by DALLAS in America. With a built-in real-time clock/calendar and a 31-byte static RAM, it can communicate with MCU throughsimple serial interfaces. The real-time clock/calendar circuit provides information aboutsecond, minute, hour, day, week, month, and year. DS1302 can automatically adjust thenumber of days per month and days in leap year. You can determine to use a 24-hour or12-hour system by AM/PM selection. It can simply communicate with MCU in a synchronousserial way and only needs to use three interface cables: Reset (RST) cable, I/O data (SDA)cable and serial clock (SCL) cable.

84

The schematic diagram

Experimental ProceduresStep 1: Build the circuitThe wiring between DS1302 and SunFounder Uno board is as shown below:

DS1302 SunFounder Uno

SCL 7

SDA 6

RST 5

VCC 5v

GND GND

85

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you can see the current date and time displayed on the I2C LCD1602.

86

Lesson 23 Humiture DetectionIntroductionThe digital temperature and humidity sensor DHT11 is a composite sensor that contains acalibrated digital signal output of temperature and humidity. The technology of adedicated digital modules collection and the temperature and humidity sensingtechnology are applied to ensure that the product has high reliability and excellent long-term stability.

The sensor includes a resistive sense of wet component and an NTC temperaturemeasurement device, and is connected with a high-performance 8-bit microcontroller.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Humiture sensor module- 1 * I2C LCD1602- 1 * 3-Pin anti-reverse cable- 1 * 4-Pin anti-reverse cable- 1 * Dupont wire (F to F)

Experimental PrincipleOnly three pins are available for use: VCC, GND, and DATA. The communication processbegins with the DATA line sending start signals to DHT11, and DHT11 receives the signals andreturns an answer signal. Then the host receives the answer signal and begins to receive 40-bit humiture data (8-bit humidity integer + 8-bit humidity decimal + 8-bit temperatureinteger + 8-bit temperature decimal + 8-bit checksum).

87

For more information, please refer to the datasheet of DHT11.

The schematic diagram

Experimental ProceduresStep 1: Build the circuit

88

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

89

Now, you can see the current value of humidity and temperature displayed on the I2CLCD1602.

90

Lesson 24 RelayIntroductionAs we know relay is a device which is used to provide connection between two or morepoints or device in response to the input signal applied. In another words relay provideisolation between the controller and the device as we know devices may work on AC aswell as on DC. However, they receive signals from microcontroller which works on DChence we require a relay to bridge the gap. Relay is extremely useful when you need tocontrol a large amount of current or voltage with small electrical signal.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Relay module- 1 * 3-Pin anti-reverse cable

Experimental PrincipleRelay – There are 5 parts in every relay:

1. Electromagnet – It consists of an iron core wounded by coil of wires. When electricity ispassed through, it becomes magnetic. Therefore, it is called electromagnet.

2. Armature – The movable magnetic strip is known as armature. When current flowsthrough them, the coil is it energized thus producing a magnetic field which is used tomake or break the normally open (N/O) or normally close (N/C) points. And the armaturecan be moved with direct current (DC) as well as alternating current (AC).

3. Spring – When no currents flow through the coil on the electromagnet, the spring pullsthe armature away so the circuit cannot be completed.

4. Set of electrical contacts – There are two contact points:

. Normally open - connected when the relay is activated, and disconnected when it isinactive.

. Normally close – not connected when the relay is activated, and connected when it isinactive.

5. Molded frame – Relays are covered with plastic for protection.

91

Working of Relay

The working principle of relay is simple. When power is supplied to the relay, currents startflowing through the control coil; as a result, the electromagnet starts energizing. Then thearmature is attracted to the coil, pulling down the moving contact together thusconnecting with the normally open contacts. So the circuit with the load is energized. Thenbreaking the circuit would a similar case, as the moving contact will be pulled up to thenormally closed contacts under the force of the spring. In this way, the switching on and offof the relay can control the state of a load circuit.

So in this experiment, hook the SIG to the SunFounder Uno board. Send a low level to SIG;the PNP transistor is energized and the coil of the relay is electrified. Thus, the normally opencontact of the relay is closed, while the normally closed contact of the relay will be off thepublic port. Send a high level to SIG; the transistor will be de-energized and the relay willrestore to the initial state.

92

Experimental ProceduresStep 1: Build the circuit

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you may hear the ticktock. That's the normally closed contact opened and thenormally open contact closed.

93

Lesson 25 Stepper MotorIntroductionStepper motors, due to their unique design, can be controlled to a high degree ofaccuracy without any feedback mechanisms. The shaft of a stepper, mounted with a seriesof magnets, is controlled by a series of electromagnetic coils that are charged positivelyand negatively in a specific sequence, precisely moving it forward or backward in small"steps".

There are two types of steppers, unipolars and bipolars, and it is very important to knowwhich type you are working with. In this experiment, we will use a unipolar stepper.

SunFounder board or other MCUs cannot directly drive stepper motors. A driver circuit isnecessary, so we use a stepper motor driver board (as shown in the following picture) todrive the stepper motor.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Stepper Motor- 1 * Stepper Motor Driver- Several jumper wires

94

Experimental PrincipleThe stepper motor is a four-phase one, which uses a unipolarity DC power supply. As longas you electrify all phase windings of the stepper motor by an appropriate timing sequence,you can make the motor rotate step by step. The schematic diagram of a four-phasereactive stepper motor is as shown below:

In the figure, in the middle of the motor is a rotor – a gear-shaped permanent magnet.

Around the rotor, 0 to 5 are teeth. Then more outside, there are 8 magnetic poles, with

each two opposite ones connected by coil winding. So they form four pairs from A to D,

which is called a phase. It has four lead wires to be connected with switches SA, SB, SC, and

SD. Therefore, the four phases are in parallel in the circuit, and the two magnetic poles in

one phase are in series.

Here's how a 4-phase stepper motor works:

At the beginning, switch SB is power on, switch SA, SC, and SD is power off, and B-phase

magnetic poles align with tooth 0 and 3 of the rotor. At the same time, tooth 1 and 4

generate staggered teeth with C- and D-phase poles. Tooth 2 and 5 generate staggered

teeth with D- and A-phase poles. When switch SC is power on, switch SB, SA, and SD is power

off, the rotor rotates under magnetic field of C-phase winding and that between tooth 1

95

and 4. Then tooth 1 and 4 align with the magnetic poles of C-phase winding. While tooth 0

and 3 generate staggered teeth with A- and B-phase poles, and tooth 2 and 5 generate

staggered teeth with the magnetic poles of A- and D-phase poles. The similar situation

goes on and on. Energize the A, B, C and D phases in turn, and the rotor will rotate in the

order of A, B, C and D.

The four-phase stepper motor has three operating modes: single four-step, double four-step,

and eight-step. The step angle for the single four-step and double four-step are the same,

but the driving torque for the single four-step is smaller. The step angle of the eight-step is

half that of the single four-step and double four-step. Thus, the eight-step operating mode

can keep high driving torque and improve control accuracy. In this experiment, we let the

stepper motor work in the eight-step mode.

To apply the motor in the circuit, a driver board needs to be used. Stepper Motor Driver-

ULN2003 is a 7-channel inverter circuit. That is, when the input end is at high level, the

output end of ULN2003 is at low level, and vice versa. If we supply high level to IN1, and low

level to IN2, IN3 and IN4, then the output end OUT1 is at low level, and all the other output

ends are at high level. So D1 lights up, switch SA is power on, and the stepper motor rotates

one step. The similar case repeats on and on. Therefore, just give the stepper motor a

specific timing sequence, it will rotate step by step. The ULN2003 here is used to provide

particular timing sequences for the stepper motor.

The internal schematic diagram of the Stepper Motor Driver is as follows.

96

Experimental ProceduresStep 1: Build the circuitThe wiring between Stepper Motor Driver board and SunFounder Uno board:

Stepper Motor Driver SunFounder Uno

D 11

C 10

B 9

A 8

GND GND

VCC 5v

97

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you should see the rocker arm of the stepper motor spin clockwise andcounterclockwise alternately.

98

Lesson 26 Servo

IntroductionServo is a type of geared motor that can only rotate 180 degrees. It is controlled by sendingelectrical pulses from your SunFounder board. These pulses tell the servo what position itshould move to.

A servo has three wires: the brown wire is GND, the red one is VCC, and the orange one issignal line.

Components- 1* SunFounder Uno board- 1 * USB data cable- 1 * Servo- Several jumper wires (M to M)

Experimental Principle

ServoA servo is generally composed of the following parts: case, shaft, gear train, adjustablepotentiometer, DC motor, and control circuit board.

It works like this: The SunFounder Uno board sends out PWM signals to the servo, and thenthe control circuit in the servo receives the signals through the signal pin and controls themotor inside to turn. As a result, the motor drives the gear chain and then motivates theshaft after deceleration. The shaft and adjustable potentiometer of the servo areconnected together. When the shaft rotates, it drives the pot, so the pot outputs a voltagesignal to the circuit board. Then the board determines the direction and speed of rotationbased on the current position, so it can stop exactly at the right position as defined andhold there.

99

Experimental ProceduresStep 1: Build the circuit

The schematic diagram

100

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, you can see the rocker arm of the servo rotate and stop at 90 degrees (15 degreeseach time). And then it rotates in the opposite direction.

101

102

Lesson 27 Joystick PS2IntroductionA joystick is an input device consisting of a stick that pivots on a base and reports its angleor direction to the device it is controlling. Joysticks are often used to control video gamesand robots. A Joystick PS2 is used here.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Joystick PS2 module- 1 * 5-Pin anti-reverse cable

Experimental PrincipleThis module has two analog outputs (corresponding to X and Y coordinates) and onedigital output representing whether it is pressed on Z axis.

103

Experimental ProceduresStep 1: Build the circuitThe wiring between the Joystick PS2 and SunFounder Uno board:

Joystick PS2 SunFounder Uno

GND GND

VCC 5V

Bt 7

x A0

y A1

104

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, push the joystick and the coordinates of X and Y axes displayed on Serial Monitor willchange accordingly; press down the joystick, and the coordinate of Z=0 will also bedisplayed.

105

Lesson 28 Infrared-Receiver

IntroductionAn infrared-receiver is a component which receives infrared signals and canindependently receive infrared ray and output signals compatible with TTL level. It's similarwith a normal plastic-packaged transistor in size and it is suitable for all kinds of infraredremote control and infrared transmission.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * Infrared-receiver module- 1 * Remote controller- 1 * 3-Pin anti-reverse cable

Experimental PrincipleControl a certain key (for example, Power key) via a remote controller by programming.When you press the key, infrared rays will be emitted from the remote control and receivedby the infrared receiver, and the LED on the SunFounder Uno board will light up. Connectan LED to pin 13 on the SunFounder Uno board so that you can see remotely whether thePower key is pressed down.

106

Experimental ProceduresStep 1: Build the circuit

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, press the Power key of a remote control, and both the LED attached and thatconnected to pin 13 on the SunFounder Uno board will light up. Then press any other key,and the LEDs will go out.

107

Lesson 29 RFID Entrance Guard System

IntroductionRFID is short for radio frequency identification. It is a wireless application to transfer data inthe purpose of identifying and tracking tags. In this experiment, we will use an RFID module,a relay, and an I2C LCD1602 to assemble an entrance guard system.

Components- 1 * SunFounder Uno board- 1 * USB data cable- 1 * RFID module- 1 * RFID key tag- 1 * Relay- 1 * I2C LCD1602- Several jumper wires- 1 * 3-Pin anti-reverse cable- 1 * 4-Pin anti-reverse cable- 1 * Breadboard

Experimental PrincipleFirst, you need to know the ID of the RFID key tag and write the ID to the rfidTest file.Compile the code. We can see "Welcome!" display on the I2C LCD1602. Swipe the RFID keyring on the RFID module. If the password is correct, the normally open contact of the relaywill be closed and the LCD will display a string “ID:5AE4C955” "hello SunFounder", and then"Welcome!" two seconds later; if the password is incorrect, the normally open contact ofthe relay will be disconnected and the LCD will display a string "Hello unknown guy" , andthen "Welcome!" two seconds later

108

Note: For this module, please use a 3.3V power supply, or it will get burnt.

Experimental ProceduresStep 1: Build the circuitThe wiring between RFID and SunFounder Uno is as follows:

RFID SunFounder Uno

VCC 3.3V

RST 2

GND GND

MISO 3

MOSI 4

SCK 5

NSS 6

IRQ 7

The wiring between I2C LCD1602 and SunFounder Uno:

I2C LCD1602 SunFounder Uno

GND GND

VCC 5v

SDA A4

SCL A5

The wiring between Relay Module and SunFounder Uno:

Relay Module SunFounder Uno

SIG 8

VCC 5V

GND GND

109

Step 2: Open the code file getId

Step 3: Select the correct Board and Port, and upload the sketch to the SunFounder Uno

board

Then place the RFID key tag in the induction zone of the RFID module. You will see thefollowing values printed on Serial Monitor:

110

Step 4: Now, you may know the ID of your RFID key tag (e.g. my magnetic card ID is5AE4C955).

Open the rfidTest file and replace the ID in the sketch with the ID you just note down (dividethe ID into four parts and fill them according to the following format), as follows:

Step 5: Upload this sketch to SunFounder Uno board

Now, swipe the RFID key tag on the RFID module. If the password is correct, the LCD willdisplay a string “ID:5AE4C955” “Hello SunFounder”, and then display "Welcome!" twoseconds later. If the password is incorrect, the LCD will display a string “Hello unknown guy”,and then display "Welcome!" two seconds later.

111

112

Lesson 30 Password Lock

IntroductionAfter having learnt so many modules separately, let’s use these modules together to makesome funny interactive works! In this lesson, we will use an I2C LCD1602, a relay module, apotentiometer and a keypad to assemble a simple password lock. It is built based on aSunFounder microcontroller and can be applied in security doors.

Components- 1* SunFounder Uno board- 1 * USB data cable- 1 * Relay module- 1 * I2C LCD1602- 1 * Keypad- Several jumper wires- 1 * 3-Pin anti-reverse cable- 1 * Breadboard

Experimental PrincipleSet a password (for example 123456) by programming. The I2C LCD1602 displays"Welcome!" after power on. At this point, the normally open contact of the relay isdisconnected, and the indicator LED on the relay module keeps off. Press the asterisk "*"key to input the password, and then press "#".

If the password is correct, the normally open contact of the relay will be closed and theindicator light will be on. The I2C LCD1602 will display "Input Correctly" "Please Come In". Ifyou input other contents, the I2C LCD1602 will display "Input Error!" "Please Again", and therelay will keep the initial state. Two seconds later, the I2C LCD1602 displays "Welcome!".

113

NOTE: Through programming, we've set the four keys in the first row (with pins at the top asshown in the figure above) as 1, 2, 3, 4; those in the second row as 5, 6, 7, 8; in the third as 9,A, B, C; and in the fourth as D, *, 0, #.

Experimental ProceduresStep 1: Build the circuitThe wiring between the I2C LCD1602 and the SunFounder board:

I2C LCD1602 SunFounder Uno

GND GND

VCC 5v

SDA A4

SCL A5

The wiring between the relay module and the SunFounder board:

Relay Module SunFounder Uno

SIG 3

VCC 5V

GND GND

114

The wiring between the keypad and the SunFounder board:

Keypad SunFounder Uno

X1 8

X2 9

X3 10

X4 11

Y1 4

Y2 5

Y3 6

Y4 7

Step 2: Open the code file

Step 3: Select the correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

115

Now, the I2C LCD1602 will display "Welcome!" after power on. At this point, the indicatorLED on the relay keeps off. When you press "*" key, it will prompt "Input Your Code:". If youenter “123456” and press “#” key to confirm, the indicator LED on the relay module will lightup. The I2C LCD1602 will display "Input Correctly" "Please Come In". Two seconds later,"Welcome!" will be displayed on the I2C LCD1602. But if you input other contents, it willdisplay "Input Error!" "Please Again", and the relay will keep the initial state. Two secondslater, the I2C LCD1602 displays "Welcome!".

Tips: For any TECHNICAL questions, add a topic under FORUM section on our websitewww.sunfounder.com and we'll reply as soon as possible. For NON-TECH questions like orderissues, please email [email protected].

116

Copyright Notice

All contents including but not limited to texts, images, and code in this manual are ownedby the SunFounder Company. You should only use it for personal study, investigation,enjoyment, or other non-commercial or nonprofit purposes, under the related regulationsand copyrights laws, without infringing the legal rights of the author and relevant rightholders. For any individual or organization that uses these for commercial profit withoutpermission, the Company reserves the right to take legal action.