Building a Smart Home system - Internet of Things - ثاني ثانوي
Part 1
1. IoT Fundamentals
2. The IoT in Our Lives
3. Building IoT applications with Arduino
4. Building an IoT cloud application
Part 2
5. IoT Advanced Applications
6. ++IoT Programming With C
7. IoT messaging
8. IoT Wireless Sensor Network Simulation
61771 3. Building IoT Applications with Arduino In this unit, you will learn about the basic properties of an Internet of Things application and how to design and build practical applications using an Arduino microcontroller and the Tinkercad Circuits environment. Learning Objectives In this unit, you will learn to: > Recognize the components of microcontrollers and how to program them. > Measure data collected from various input sensors. > Understand how data from sensors and algorithms from programs work together. > Use data from sensors to trigger alerts and automated responses. > Utilizing a gas sensor to create a gas leak alarm for environments with hazardous conditions. > Design IoT circuits with an Arduino microcontroller in Tinkercad Circuits. > Program the Arduino microcontroller using the block-based coding language In Tinkercad Circuits. Tools >Autodesk Tinkercad Circuits
In this unit, you will learn about the basic proper�es of an Internet of Things
Autodesk Tinkercad Circuits
Link to den Lesson 1 Building a Smart Home System www.len edu Arduino Microcontrollers Microcontrollers are designed for single-board minicomputers rather than for desktop or personal computers. For example, Arduino produces entirely self-contained microcontrollers, often with a set of built-in microprocessors. Arduino microcontrollers are characterized by their extensive functionality. However, they are limited in functionality compared to personal computers, as they are designed to perform only simple tasks. Let's take a look at the most commonly used Arduino boards: Chagaria Arduino Nano Arduino Due 8 Figure 3.E: Arduino microcontroller boards Table 3.1: Specifications of Arduino models Arduino UNO Arduino Portenta Model USB Type 1/0: Clock speed Flash memory SRAM memory Arduino Nano 33 Mini-B 26 pins 48 MHz Arduino UNO R3 Type K 20 pins 15 MHZ Arduino Due Micro-B 68 pins Arduino Portenta H7 Type C 80 pins 84 MHz 480 MHz 256 KB 32 KB 512 KB Up to 128 MB 32 KB Z KB 96 KB up to 64 MB وزارة التعليم 2173-1465 INFORMATION Compared with personal computers or laptops, microcontrollers can be manufactured quickly and easily reducing the production cost significantly.
Arduino Microcontrollers
Table 3.1: Specifica�ons of Arduino models
Compared with personal computers or laptops,
Arduino UNO R3 The Arduino UNO R3 is a microcontroller board based on the ATmega microcontroller. It has 14 digital input/output pins, of which 6 can be used as PWM (Pulse Width Modulation) outputs and 2 are used for transmiting (Tx-1) and receiving serial data (RxD), 6 analog inputs, a USB connection, a power jack, and a reset button. External Power Supply 3.3 Volt Power Pin 5 Volt Power Pin 1}) } Ground Pins IND Analog In Pins (0-5) ANALOG! Digital Ground Integrated LED Reset Button USB Plug 1000 GND O UNO DIGITAL (PWM- 18 ARDUINO ATmega328P Microcontroller yuu 32 Arduino Un The ATM ga328P is a single-chip microcontroller that Is freque..tly found in Arduino products. It achieves high performance with low power consumption. وزارة التعليم 173-1445 70 Digital 1/0 Pins (2-13) Power Indicator LED
Arduino UNO R3
Some Examples of External Sensors for Microcontrollers Soil Moisture Sensors Soil Moisture Sensors are sensors that measure the soil's volumetric water content. Since direct measurement involves a significant amount of soil processing, soil moisture is measured indirectly utilizing other properties of the soil, such as electrical resistance (the greater the moisture the lower the resistance). Soil Moisture Sensors are crucial in agriculture and are used for monitoring applications such as irrigation control in industry, watering house and office plants as well as landscape irrigation. Soil Moisture Sensor Temperature Sensors Simulator component Figure 33: Soil Moisture Sensor Schematic symbol The TMP36 temperature sensor is a temperature measuring device that generates an analog output voltage proportional to the temperature which can be simply converted into a temperature reading in degrees Celsius. This sensor can measure temperatures in the range of -40 to 125 C. The TMP36 is used mainly in applications involving thermostats and temperature measurement, and since it doesn't need external calibration, it can be used without any additional parts. PIR Sensors Temperature Sensor Simulator component Schemanc symbol Figure 3.4 Temperature Senco Passive Infrared (PIR) sensors are electronic sensors that can detect objects in a specific Field Of View (FOV). They work by measuring the infrared radiation signals that are present in the FOV that they examine. PIB sensor Gas Sensors Simulator component Haure 3.5. PIR Schematic symbol Gas Sensors are chemical resistors that detect high levels of smoke and other gases, such as propane, hydrogen, and carbon monoxide. When the gas comes into contact with the resistor's material, its electrical resistance changes. These sensors can detect concentrations between 200 and 10,000 ppm. They are used for monitoring areas where there is a risk of fire or poisonous gas emissions. وزارة التعليم 125-1985 Gas Sensor Simulator component Figure 3 &: Gas Sensor Schematic symbol 71
Some Examples of External Sensors for Microcontrollers
72 Build a Smart Home System Every day, houses become increasingly equipped with smart sensors to enhance the quality of our life and make our activities easier. One "smart" technology is implementing home lights that automatically turn on and off by sensing a human presence in the room. In this example, you will use an Arduino UNO R3 microcontroller to simulate, in Tinkercad, an automatic room lighting system for implementation in a Smart Home. You will use 2 PIR sensors that detect if an object has entered the field of view (FOV) in either of two rooms. When there is a person in the FOV of a sensor, its LED will light up and when they leave, the LED light will turn off. The second sensor will represent another room waiting for a person to enter. For this project you will use the following components: • 1 Arduino UNO R3 • 2 Passive Infrared (PIR) Motion Sensors • 2 LEDS • 2 Resistors ⚫1 Breadboard Small Components that you will use in this project وزارة التعليم PIR Sensors OO UNO Arduino Uno R3 0 Resistors LEDs Breadboard Small Figure 3.7 Smart Home project components
Build a Smart Home System
Now, let's begin a new circuit design in Tinkercad. Tinkercad Circuits is a web-based simulator to create and program prototypes of electronic circuits with microcontrollers like Arduino. Start Tinkercad Circuits at https://www.tinkercad.com and select Circuits to open the main window of the simulator. Go to Tinkercad's dashboard, Type your code. Start the simulation, G Exercise 1 Rotate Switch between Circuit View, Schematic View and Component List Ba Set the name of the design. Edit the components using the main toolbar S Find the components for your design In the components library. Center and zoom the design to fill the window. وزارة التعليم 2173-1765 Place all the components of the circuit in the workplane Figure 3. Tinkercad Circuits main window > 1 非 Sine To 73 23
Now, let's begin a new circuit design in Tinkercad.
74 Add a component to the design by clicking on a component in the Components library and then clicking anywhere on the workplane. Click on the Delete button, to delete a selected component The inspector panel lets you edit the properties of a component. Exercis 3 www.florca.commun Click on the "Zoom to fit" button so the "readboard entered and fills the workplane. وزارة التعليم 2173-176 Click here to hide the components panel button. Figure 3.9 Edit components 0
Add a component to the design by clicking on a component
Find the components that you will need for this project, and move them to the workplane. Find and add an Arduino UNO R3, two PIR sensors, two LEDs and two resistors, and a Breadboard Small in the workplane. To add components to the workplane: > Find the Arduino UNO R3 in the components library and drag and drop it in the workplane. > Find the Breadboard Small in the components library and drag and drop it in the workplane. Application درارة التعليم 2 00 DO 3 0 1 t Figure 3.10 Add components to the workplane 75
Find the components that you will need for this project,
Now, you will add two resistors to the breadboard. To add resistors to the breadboard; > Drag and drop the resistors from the components library onto the Breadboard. > From the inspector panel, set Resistance for both resistors to 220 and set the unit n. 0 Smn Home Application 76 وزارة التعليم 2 34 Cone 3 Figure 311 Add resistors to the breadboard 0 Sand To
Now, you will add two resistors to the breadboard.
Continue by adding two LEDs to the breadboard and two PIR Sensors. You need to connect the anode of each LED in series with its corresponding resistor. To add components to the breadboard: > Drag and drop the PIR Sensors from the components library, and place them on the Breadboard. > Drag and drop the LEDs from the components library, and place the anodes of each LED in parallel with the corresponding resistor in the Breadboard. Smart Home Application PARALLAX ப REV & 555-28027 وزارة التعليم 2 CZA GO 非 PARALLAX I, { R R Figure 3:12 Add LEDs to the breadboard 555-18227 + 77
Continue by adding two LEDs to the breadboard and two PIR Sensors.
Next, wire the Arduino UNO R3 to the breadboard by connecting the SV pin to the positive column and the GND pin to the negative column. To wire the Arduino UNO R3: > Connect the Arduino UNO R3 5V pin to the positive column of the Breadboard and change the wire color to red. > Connect the Arduino UNO R3 ground pin to the negative column of the Breadboard and change the wire color to black. ΠΑ KRUDINO POWER ANALOG IN ARDZERU درارة التعليم POWER ANALOG IN 2 1 Figure 3.13: Wining Arduino UND 83 139 P G
Next, wire the Arduino UNO R3 to the breadboard by connecting the
Then, connect the LED resistors to the Arduino digital pins 6 and 7. To connect the LED resistors: > Connect one of the resistors to Digital pin 6 of the Arduino UNO R3 and change the wire color to brown. > Connect the other resistor to Digital pin 7 of the Arduino UNO R3 and change the wire color to brown. > Connect the two LED cathodes to the negative column of the breadboard. DIGITAL (FWM-) XO UNO QUINO ROWER ANALOG IN 222232 وزارة التعليم 1 Figure 3.14 Witing LED Resistor pairs 3 2 79
Then, connect the LED resistors to the Arduino digital pins 6 and 7.
80 Finally, to complete the connections, you need to connect PIR "Room 1" and PIR "Room 2" to the Arduino Uno R3 and to the negative and positive columns of the breadboard. To wire PIR sensors: > Connect the PIR sensor Signal pin to Digital pin 8 and change the wire color to green. > Connect the PIR sensor Power pin to the positive column of the Breadboard Small and change the wire color to red. > Connect the PIR sensor Ground pin to the negative column of the Breadboard Small and change the wire color to black. > Connect the PIR sensor Signal pin to Digital pin 9 and change the wire color to orange. > Connect the PIR sensor Power pin to the positive column of the Breadboard Small and change the wire color to red. > Connect the PIR sensor Ground pin to the negative column of the Breadboard Small and change the wire color to black. Just like when you're coding, it's important to add comments to your work. In Tinkercad, you can add comments on the workplane. For example, you can add comments to indicate the two rooms by using the note tool. Smart Home Application Mote thol OO LIND ARQUIND о ANGLOIN وزارة التعليم Comments Fo 2 1 3 4 6 Figure 3.15 Wiring PIR sensors
Finally, to complete the connections, you need to connect PIR "Room 1" and PIR "Room 2"
Code Blocks Now you have finished with the component preparation, let's explore the programming environment that you will use in this unit. Tinkercad provides Block-Based Coding technology to simplify microcontroller programming. Toggle code editor Block categories 22 Clady Start Bimulation Send To Blocks 土 1 (Arduino Uno R3) com 1 Room 2 tepul Notation Math Code blocks Make the code blocks larger or smaller, or reset their size. Figure 16-Codu blacks The trash bin is used to remove unwanted blocks. Blocks categories Output The Output blocks category allows you to define the digital and analog pins and send commands to the components of the microcontroller. Input The Input blocks category allows you to read data from the microcontroller. Notation The Notation blocks category allows you to use comments to your code. Control The Control blocks category allows you to add events, define loops to repeat actions and use statements to take decisions. Math The Math blocks category allows you to use mathematical symbols and operations. Variables The Variables blocks category allows you to create variables. وزارة التعليم 2173-1465 INFORMATION Tinkercad's visual code blocks are useful for creating programs for the Arduino while avoiding common errors such as syntax errors, mistyping function names, forgetting a semicolon (), and so on. 81
Code Blocks
Tinkercad's visual code blocks are useful for creating programs
82 Let's take a look at some important blocks that you will use for this project: The forever and the if () then else blocks can be found in the Control blocks category. The forever block is essential to every program, because it is the block in which the rest of the commands are located. All other blocks inside it will run forever, in sequence, until the loT device is switched off. The if () then else block checks whether the criteria of the condition are met. If so, the microcontroller executes the blocks between if () then and else. Otherwise, the blocks below else are executed. Control E The set pin () to () block can be found in the Output blocks category. The set pin () to () block can set a digital or analog pin state to High or Low Analog pins can have a range of voltage values from OV to 3,3V or 5V. This means that when Interpreted by the program, these voltages correspond to a large variety of values. Output Spill L to HIGH- The read digital pin () and the read analog pin () blocks can be found in the Input blocks category.. The read digital pin () block is a boolean block that reads the state of the digital pin on the device ("High" or "Low"). The read analog pin () block can read a voltage level that ranges from the supply voltage of your board, 3.3V or 5V, down to 0V (GND). CN Digital pins have two states; High, when voltage Layes through 13.3V or 5V), and Low, when it does not (OV). 1 two states are Interpreted by the program as 1 (True) and 0 (False) respectively. Input read dignal pin 0- rend analog ph AD -> 15 - כקו
Let's take a look at some important blocks that you will use for this project:
Let's Take a Look at the Code. Your program will execute forever and implement two if/else blocks, one for Room 1 and the other for Room 2. The PIR sensors will check for movement inside the rooms. If a PIR sensor detects a "person" it will turn its LED on and when the PIR sensor stops sensing the "person" then it will turn its LED off. This circuit simulates a Smart Home Application that turns the lights on automatically when someone enters a room. Blocks وزارة التعليم Mum raad ditil tin HIGH- LOV Start Simulation Bend To I (Arduino Uno R3) ▼ Blocks inside the forever block will run continuously in sequence until you stop the simulation HIGH rena diyttal an 9 HIGH in 7 In HIGH▾ pin 7 to LOW- Check for any movements in Room 1. Check for any movements in Room 2 Figure 3.17: Code Implementation 83
Let's Take a Look at the Code.
Start the simulation by pressing the Start Simulation button to test your program. Move the presence indicator Inside the PIR Sensor FOV to switch on the lights Lights on in Room 1. e Lights on in Room 2. mor Θ 84 Agure 318 Program simulation Start Simulation 1 e
Start the simulation by pressing the Start Simula�on bu�on to test your program.
Exercises 1 Fill the empty boxes with the missing component names. External Power Supply 0 100 وزارة التعليم -14650 SLET 1 до L AS AIRF ∞ (UNO DIGITAL IPWM-) ARDUINO ATmega328P Microcontroller AND Integrated LED Power Indicator LED 715
Fill the empty boxes with the missing component names.
BIT 2a. Match the items in the first row with those in the second. Simulator component Name Temperature b. Write in which cases we can use each sensor. 1. Temperature 2. Gas 3. Soil Moisture TMP Gas Soil Moisture 3 What is the difference between digital and analog pins? haارة التعليم
a. Match the items in the first row with those in the second.
What is the difference between digital and analog pins?
BIT 2a. Match the items in the first row with those in the second. Simulator component Name Temperature b. Write in which cases we can use each sensor. 1. Temperature 2. Gas 3. Soil Moisture TMP Gas Soil Moisture 3 What is the difference between digital and analog pins? haارة التعليم