Programming Arduino with Python - Internet of Things - ثاني ثانوي

136 Link to digital lesson Lesson 2 Programming Arduino with Python www.len.edusa Smart Garden with an Arduino Due to climate change, smart gardening is promoted as an agricultural method that is both very sustainable and scalable. To cover the agricultural needs of an increasing population with climate change in mind, there need to exist more efficient agricultural methods such as smart gardening. We will simulate an Arduino circuit that monitors a smart garden and sends data to an online cloud platform. The Arduino will constantly send data to the cloud and if a specific set of temperature and moisture conditions are met, we will simulate turning on the watering system. We will first simulate the circuit in Tinkercad Circuits to show clearly the circuit connections and then you will use the simulation to guide you to build the exact same circuit with a real Arduino microcontroller. You will need the following components: • 1 Arduino Uno R3 1 Breadboard Small ⚫ 1 Soil Moisture Sensor ⚫1 TMP Temperature Sensor • 1 DC Motor TMP 715 Cloud platform A cloud platform is a server in an Internet-based data center that makes it possible for software and hardware services to work together remotely and in large distributions. TMP Temperature Sensor DC Motor Components that you will use in this project ارنا التعليم PLIFI 21173-1985 Soil Moisture Sensor 8 LIND Breadboard Small Arduino Uno R3 Figure 4 17 Smart garden project components

Lesson 2: Programming Arduino with Python

Smart Garden with an Arduino

To load the components: > Find the Arduino Uno R3 from the components library and drag and drop it into the workplane. > Find the Breadboard Small from the components library and drag and drop it into the workplane. Smart Garten وزارة التعليم 2 OO UNO 1 Figure 4.18: Loading the components 0 = A 7 Code

Lesson 2: Programming Arduino with Python

Loading the components:

(38 Now you will connect the Arduino to the breadboard. To connect the Arduino to the Breadboard: > Connect the Arduino Uno R3 GND to the negative column of the Breadboard Small and change the wire color to black. > Connect the Arduino Uno R3 5V to the positive column of the Breadboard Small and change the wire color to red. Smart Garden TX FLX ARDUINO وزارة التعليم DIGITAL (PWM-> UNO RX+0 ON POWER ANALOG IN 2 1 Figure 4 19: Connecting the Arduino to the Breadboard 2 fghij

Lesson 2: Programming Arduino with Python

Now you will connect the Arduino to the breadboard.

Now you will connect the DC motor to a digital port in the Arduino. To connect the DC motor: > Connect the Arduino Uno R3 GND to Terminal 1 of the DC motor and change the wire color to black. > Connect the Digital pin 3 to the Terminal 2 of the DC motor and change the wire color to red. The DC motor will emulate opening a watering system valve which would be activated when a specific set of conditions are met. 1 وزارة التعليم +- @ ཀ ༤ r DIGITAL PWM-I O UNO ARDUINO 2 DIGITAL PWM-) 00 UNO ARDUINO POWER ANALOG IN POWER ANALOGIN Figure 4.20, Connecting the DC motor 150

Lesson 2: Programming Arduino with Python

Now you will connect the DC motor to a digital port in the Arduino.

100 Now you will connect the Temperature sensor to an analog port in the Arduino. To connect the TMP Temperature sensor: > Connect the Power end of the TMP sensor to the positive column of the Breadboard Small and change the wire color to red. > Connect the Vout end of the TMP sensor to the Analog pin A2 of the Arduino and change the wire color to green. > Connect the GND end of the TMP sensor to the negative column of the Breadboard Small and change the wire color to black. Smart Garden DIGITAL (PWM-) OO (UNO) ARDUINO POWER ANALOG IN 3222222 وزارة التعليم fghil Figure 4.21: Connecting the TMP Temperature sensor TMP 2 3 0

Lesson 2: Programming Arduino with Python

Now you will connect the Temperature sensor to an analog port in the Arduino.

ER Finally you will connect the Soil Moisture Sensor to an analog port in the Arduino. To connect the Soil Moisture Sensor: > Connect the Power end of the Soil Moisture Sensor to the positive column of the Breadboard Small and change the wire color to red. > Connect the Ground end of the Soil Moisture Sensor to the negative column of the Breadboard Small and change the wire color to black. > Connect the Signal end of the Soil Moisture Sensor to the Analog pin A4 of the Arduino and change the wire color to green. ANALOGIN وزارة التعليم 3 Figure 4.22 Connecting the Soll Moisture sensor 2 Soil Moisture Sensor 非

Lesson 2: Programming Arduino with Python

Finally you will connect the Soil Moisture Sensor to an analog port in the Arduino.

Complete Circuit 00 The components are connected to the following pins: D3 وزارة التعليم 2173-1465 पर AZ TMP A4 Figure 4 23 Pins connected to components

Lesson 2: Programming Arduino with Python

Complete Circuit

This photo represents what the physical circuit will look like. Physical Circuit Moisture Sensor v1.2 Capacitive Soil زارة التعليم 123-1985 Assume that the DC motor is connected to a watering pump or valve. B Figure 4 24- Photo of the physical circuit

Lesson 2: Programming Arduino with Python

Physical Circuit

184 Programming the Arduino Smart Garden Sensors and Motor You will now program the Arduino to read the temperature and soil moisture sensor pins. If a specific set of temperature and moisture values are met, then the DC motor will be activated through a function, turning on for 5 seconds, then stopping, mimicking the automatic monitoring and watering of a plant in a smart garden. Open PyCharm, create a new Python file and import the required libraries. import pyfirmata import time Setup the communication port and the required pins. communication_port = 'COM4' dc_motor_pin = board.get pind:3:0) temperature sensor_pin moisture sensor_pin = = board, get pin( 'a:2:1') board.get_pin('a:4:1') Setup the communication between PyFirmata and the board. board = pyfirmata Arduino (communication_port) it = pyfirmata.util.Iterator(board) it.start() Implement the following method to control the DC motor. def water plant(de_motor_pin): print("Watering plant ---") de_motor_pin.write(1) time.sleep(5) de_motor_pin.write(0) This method sends a HIGH digital signal for 5 seconds to the BC motor and then sends a LOW digital signal to stop the rotation, ورات اللي ليتر 13 - כקו

Lesson 2: Programming Arduino with Python

Programming the Arduino Smart Garden Sensors and Motor

10 - כשוע Create an infinite loop under which we will write our code. while True: #add your code here Read the unprocessed temperature and moisture input values that you get from the analog pins. temperature_value = temperature sensor_pin.read() moisture_value = moisture sensor_pin.read() Check if the input values from the pins are null. The rest of the logic is implemented below this condition. if (temperature value is not None) and (moisture value is not None): Sometimes microcontroller sensors send null values, so we add another check so as to avoid errors in the program. Create the following variables that map the unprocessed input values to the appropriate temperature and moisture values using mathematical formulas. For the temperature, we first get the 3 first decimal digits of the analog signal that comes from the temperature sensor and we then convert that value to the voltage that is applied by the temperature sensor on the signal pin. The next step is to convert the voltage to Celsius degrees which is a specific formula for this sensor type. temperature_value = float(temperature value) + 1000 voltage (temperature value / 1024) * 5 temperature (voltage 0.5) + 100 float(moisture_value)) + 108 Converting the temperature to degrees Celsius and the moisture to a percentage. 776

Lesson 2: Programming Arduino with Python

Create an infinite loop under which we will write our code.

166 Insert the temperature and moisture conditions for the watering of the plant. if (temperature >= 24.0) and (moisture <= 40.0): water plant (de_motor_pin) If the temperature is hotter than 24 degrees Celsius and the soil moisture level is lower than 40%, activate the plant watering mechanism. Create report messages to display through the terminal when the program runs and collects data, temperature report = "Temperature + str(temperature) + "C" moisture report = "Moisture : str(round (moisture, 2)) + "%" + print(temperature_report) print (moisture report) Complete Code import time import pyfirmata board pyfirmata. Arduino('COM4") it pyfirmata.util.Iterator(board) start() A motor pinboard.get_pin('d:3:0') ة التعليم T J173-1985

Lesson 2: Programming Arduino with Python

Insert the temperature and moisture conditions for the watering of the plant.

Complete Code1

temperature sensor pinboard.get_pin('a:2ri) moisture sensor pinboard.get_pin('a:4:1') def water plant(dc_motor_pin): print("Watering plant ") de motor pin.write(1) time.sleep(5) de_motor_pin.write(0) while True: = temperature value temperature sensor pin.read() moisture_value moisture sensor_pin.read() if (temperature value is not None) and (moisture value is not None): ورات السمات ليتر temperature value = float(temperature_value) * 1000 voltage (temperature value / 1024) - 5 temperature (voltage 0.5) + 100 moisture = = * (1.6 float(moisture_value)) - * 100 if (temperature >= 24.0) and (moisture <= 40,0): water plant(de_motor_pin) temperature report = "Temperature: + str(temperature) + C" moisture report = "Moisture : " + str(round(moisture, 2)) + "%" print(temperature_report) print(moisture report) time.sleep(10)

Lesson 2: Programming Arduino with Python

Complete Code2

Exercises 1 Consider whether an analog output with Pulse Width Modulation would be a more efficient output to control a DC motor. Present your ideas below. 2 Considering the number of I/O pins of an Arduino and a micro:bit which one is a better microcontroller for a smart garden system. Present your ideas below. 3 Explain why the sensor input values taken from the analog pins have to be processed with a different mathematical formula depending on the sensor. وزارة التعليم

Lesson 2: Programming Arduino with Python

Consider whether an analog output with Pulse Width Modulation would be a more efficient output to control a DC motor. Present your ideas below.

Considering the number of I/O pins of an Arduino and a micro:bit which one is a better microcontroller for a smart garden system. Present your ideas below.

Explain why the sensor input values taken from the analog pins have to be processed with a different mathematical formula depending on the sensor.

4 Describe why it is important to perform checks on the collected data to see whether the sensors send null or corrupt data. 5 Consider whether a servo motor would water the plants more precisely and effectively Present your ideas below. 6 Expand the Python program with print commands to generate a report of the environment readings every 30 seconds.

Lesson 2: Programming Arduino with Python

Describe why it is important to perform checks on the collected data to see whether the sensors send null or corrupt data.

Consider whether a servo motor would water the plants more precisely and effectively. Present your ideas below.

Expand the Python program with print commands to generate a report of the environment readings every 30 seconds.