Lesson 2 Circuit with Microcontroller Building a Simple Traffic Light System Microcontrollers like micro:bit can also be connected to a breadboard. This can make it easier to design more complex circuits by allowing more components to be interconnected. We will begin this lesson by building a simple traffic light system with 3 LEDs and 3 resistors connected to a breadboard. Each LED will emit light briefly for 300 milliseconds before the microcontroller switches to the next LED. The LEDs are connected to the pins PO, P1 and P2 of the micro:bit. • PO-> Red LED • P1 --> Yellow LED • P2 --> Green LED The micro:bit will send a digital signal of 1 to each pin for 300 milliseconds. This will allow electric current to flow through the wires and the LEDs to emit light. The circuit and the code are illustrated below. Components that you will use in this project abcde fghij وزارة التعليم Ministry of Education 2024-1446 A 12 0:01 B B 14 13 - 14 15 15 16 <- 16 17 <-17 0 1 2 3V GND 18 <-18 19 <-19 20 <- 20 21 <- 21 22 - 22 23 <-23 24 <-24 25 <-25 26 26 27 27 28 28 29 29 30 30 abcde fghi INFORMATION A resistor is used to limit the current through the LED and to prevent excess current that can burn out the LED. Link to digital lesson www.ien.edu.sa 187

2: Circuit with Microcontroller

Building a Simple Traffic Light System

Let's start by adding the micro:bit to the workplane. To add micro:bit: > Find the micro:bit in the components library drag and drop it into the workplane. 2 and > Click on the drop down menu 3 and choose Green. 4 وزارة التعليم Ministry of Education 188 2024-1446 II 0: 2 Saving... 非 4|A < Code Start Simulation Send To micro:bit Name 3 Components Basic 3 Search Color Green Red Yellow 9V Green 4 Blue 9V Battery B 2 3V GND F- Figure 5.15: Add micro:bit > SON BATTERY CR2022 Coin Cell 3V Battery 1.5V Battery Breadboard Small 1 UNO micro:bit Arduino Uno R3

2: Circuit with Microcontroller

Add micro:bit:

Now, you will add a breadboard to the workplane. To add breadboard: > Find the breadboard in the components library 1 and drag and drop it into the workplane. 2 > Click on the rotation button three times. 3 0 0:11 وزارة التعليم Ministry of Education 2024-1446 GND 3 44 Breadboard Small Name 1 11461 Traffic Lights O All changes saved 非 < Code Start Simulation Send To Components Basic Search AA 1.3V 1 2 1.5V Battery Breadboard Small 11111 11111 All changes saved A KA Code Start Simulation Send To Breadboard Small Name 1 Basic Search Figure 5.16: Add breadboard 1.5V Battery Breadboard Small micro:bit Arduino Uno R3 Vibration Motor DC Motor 189

2: Circuit with Microcontroller

Add breadboard:

Next, you will add three LEDs to the workplane and change their colors to red, yellow and green. You will also add three resistors of 1k each to the workplane. To add LEDs and resistors: > Find the LED in the components library > and drag and drop three of them into the workplane. 2 3 > Change the colors of the second LED from red to yellow and the third LED from red to green. > Find the resistor in the components library 4 and drag and drop three of them into the workplane. > Rotate the LEDs and resistors to be horizontal. 6 Don't forget, in Tinkercad when you place a resistor in the workplane, the default value is 1km. TIN KER CAD Traffic Lights (20) 3V fghij 2 3 وزارة التعليم Ministry of Education 190 2024-1446 B GND All changes saved 非 44 < Code Start Simulation Send To LED Components Basic Name 2 3 Search a Color Yellow 4 1 Figure 5.17: Add LEDs and resistors 2 Resistor LED 5 6 Pushbutton Potentiometer

2: Circuit with Microcontroller

Add LEDs and resistors:

Continue by connecting each resistor - LED light pair in series on the breadboard. To connect components on the breadboard: 1 > Drag and connect the three LEDs and resistors to the breadboard so that each wire of the LEDs and resistors is in a hole. ② ↑ ות 23 a b c d e 1 hij 10 13 13 14 14 15 15 16 16 17 17 18 19-> 18 19 وزارة التعليم Ministry of Education 2024-1446 - 3V GND 1 2 a b c d e 2 f g h i j 1 2 3 3 4 4 5 5 6 • 6 7 7 8 8 9 • 9 10 10 11 11 12 12 13 13 Figure 5.18: Connecting components on the breadboard - 191

2: Circuit with Microcontroller

Connecting components on the breadboard:

Now you are going to use wires to connect the cathode of each LED light to the ground pin of the micro:bit. To wire cathodes to the microbit ground pin: > Connect the micro:bit ground pin to the negative column of the breadboard. 1 > Connect the three LED cathodes to the negative column of the breadboard. > Change all the wires to black to indicate the negative connections. 3 WIRE COLOR 3 Black Red Orange Yellow Green Turquoise Blue Purple Pink Brown a b c d e Grey 0:. KA Code 3V GND A 0 وزارة التعليم Ministry of Education 192 2024-1446 0 2 B 30 3V GND 1 a b c d e fghij F • 15 16 10 123456789 OFF 2 5 7 8 10 12 13 14 15 16 17 17 Figure 5.19: Wiring cathodes to the ground pin White 2

2: Circuit with Microcontroller

Wiring cathodes to the ground pin:

Finally you are going to connect the pins PO, P1 and P2 to each resistor - LED light pair. To wire pins: > Connect micro:bit PO to hole a2 of the breadboard. 1 > Connect micro:bit P1 to hole a5 of the breadboard. 2 > Connect micro:bit P2 to hole a8 of the breadboard. 3 To have a clear circuit with well-organized wires, when you have one point of a wire attached to a component, you can click on a point in the workplane to bend the wire there. Another way to bend a wire is by double clicking the wire at a specific point. وزارة التعليم Ministry of Education 2024-1446 " A a b c e 2 3V GND 1 2 3 a b c e f g h i j 2 3 4 5 6 7 8 9 10 11 Figure 5.20: Wiring pins 2 5 7 8 10 11 2 f g 193

2: Circuit with Microcontroller

Wiring pins:

Now that you have finished preparing the components, you are ready to start coding. Let's take a look at some of the commands for micro:bit pins that you can use in Python: To set which pin the micro:bit (PO, P1, or P2) sends pitch signals from: pins.analog_set_pitch_pin (AnalogPin.PO) To read a value from analog pin PO, use the following command: pins.analog_read_pin(AnalogPin.PO) 2 To write a value to a pin, use the following commands. Here we write the value from the analog pin PO to the analog pin P2. fgh وزارة التعليم Ministry of Education 194 2024-1446 po_value = pins.analog_read_pin(AnalogPin.PO) pins.analog_write_pin(AnalogPin.P2, pO_value) Analog pin values can be any number, while digital pin values must be 0 or 1. pins.digital_write_pin(DigitalPin.P1, 0) pins.digital_write_pin(DigitalPin.P1, 1)

2: Circuit with Microcontroller

Now that you have finished preparing the components

Write the following code and start the simulation. 1:0 AB 4 Simulator time: 00:01:22 2 3 4 B 5 6 2 3V GND a b c de fghij 10 10 11 •11 Code Stop Simulation Send To 1 (micro:bit) 1 def on_forever () : 8 9 10 11 pins.digital_write_pin (Digital Pin. PO, 1) basic.pause (300) pins.digital_write_pin (DigitalPin. P0, 0) pins.digital_write_pin (Digital Pin. P1, 1) basic.pause (300) pins.digital_write_pin (Digital Pin. Pl, 0) pins.digital_write_pin (Digital Pin. P2, 1) basic.pause (300) pins.digital_write_pin (DigitalPin. P2, 0) basic.pause (300) 12 basic.forever (on_forever) Figure 5.21: Test code You will observe the three LED lights flashing alternately every 300 ms. pins.digital_write_pin(DigitalPin.PO, 1) pins.digital_write_pin(DigitalPin.PO, O) pins.digital_write_pin(DigitalPin.P1, 1) pins.digital_write_pin(DigitalPin. P1, 0) pins.digital_write_pin(DigitalPin. P2, 1) pins.digital_write_pin(DigitalPin.P2, 0) وزارة التعليم Ministry of Education 2024-1446 10 195

2: Circuit with Microcontroller

Write the following code and start the simulation.

Building a Detection Alarm System We will now build a circuit that uses a PIR sensor and a piezo buzzer to create an alarm system. We will program the analog pin P2 of the micro:bit to emit analog pitch signals. The negative end of a piezo buzzer will be connected to the micro:bit ground, and its positive end will be connected to the analog pin P2 in order to receive the pitch signals. The PIR sensor will detect if an object has entered its FOV. If there is an object present, it will send a digital signal of 1 to the micro:bit pin PO. The micro:bit will then display an exclamation mark and emit a pitch sound twice with an interval of 100 milliseconds. The PIR sensor is connected to 3 wires, one for the ground, one for the power, via the 3V pin of the micro:bit, and one for the digital signal it sends to pin PO. Let's create the circuit and write the code for our project. 4A micro:bit </Code PARALLAX EV 555-28827 PIN SENSOR Figure 5.22: PIR sensor and a piezo buzzer To add micro:bit: > Find micro:bit in the components library and drag and drop it ① into the workplane. 2 > Click on the drop down menu and choose Green. 4 3 Start Simulation Send To Components Basic وزارة التعليم Ministry of Education 196 2024-1446 0: Name 3 3 Search Color Green Red Yellow 9V Green 4 Blue 9V Battery II III B 2 3V GND 2 F- Figure 5.23: Add the micro:bit > ASL VY CON BATTERY Coin Cell 3V Battery 1.5V Battery Breadboard Small P 1 UNO micro:bit Arduino Uno R3

2: Circuit with Microcontroller

Building a Detection Alarm System

Add a piezo buzzer to the workplane. To add piezo buzzer: > Find the piezo buzzer in the components library and drag and drop into the workplane. 2 > Rotate the piezo buzzer so the pins point towards the micro:bit. 3 All changes saved < Code Start Simulation Send To Components Basic Search Connect the positive end of the piezo buzzer to pin P2 of the micro:bit. This will be the analog pin that will send a pitch signal to the piezo buzzer. To wire the piezo buzzer: > Connect the positive pin of the piezo buzzer to pin 2 of the micro:bit. 2 > Connect the negative pin of the piezo buzzer 3 to the ground pin of the micro:bit. 4 B N 715 NPN Transistor LED RGB (BJT) 1 2 3V GND Diode Photoresistor وزارة التعليم Ministry of Education 2024-1446 Soil Moisture Sensor Ultrasonic Distance Sensor 2 PIR Sensor Piezo 3 TMP 125.0 mA Temperature Sensor [TMP36] Multimeter 1 Change wire to black to indicate that it is connected to ground. Figure 5.24: Add a piezo buzzer 3 [:0 1 2 3V GND 4 1 + Figure 5.25: Wire the piezo buzzer 197

2: Circuit with Microcontroller

Add a piezo buzzer to the workplane.

Add a PIR Sensor to the workplane. To add PIR Sensor: > Find the PIR Sensor in the components library and drag and drop it into the workplane. 2 > Rotate the PIR Sensor so the pins point towards the micro:bit. 3 [{111} 10065 PARALLAX 2 4/0 PIR SENSOR REV B 555-28027 PIR SENSOR REV B 555-28027 وزارة التعليم Ministry of Education 198 2024-1446 F- PARALLAX Rev B PIR SENSOR Signal Power Figure 5.26: PIR Sensor 555-28827 Ground 非 Start Simulation Send To Saving... < Code Components Basic III III Search PARALLAX 3 2 3V GND > Diode 1 Soil Moisture Sensor w Photoresistor Ultrasonic Distance Sensor PIR Sensor Piezo Figure 5.27: Add PIR Sensor TMP 125.0 mA 000 Temperature Sensor [TMP36] Multimeter

2: Circuit with Microcontroller

Add PIR Sensor:

Connect the ground end of the PIR Sensor to the GND pin of the micro:bit. To connect PIR: > Connect the PIR Sensor Ground pin 1 to the GND pin of the micro:bit. 2 The default color of a new wire is the color that you used for your previous wire. Connect the Power pin of the PIR Sensor to the 3V power source of the micro:bit. To connect PIR: > Connect the PIR Sensor Power pin 1 to the 3V power source of the micro:bit. 2 > Change the color of the wire to red. 3 وزارة التعليم Ministry of Education 2024-1446 PIR SENSOR REV B PIR SENSOR 555-28827 REV B PARALLAX 555-28027 PARALLAX 1 Figure 5.28: Connect the Ground pin of the PIR Sensor 1 3 Figure 5.29: Connect the Power pin of the PIR sensor 0: IIII 0: III 2 N 3V GND 199 GND 2

2: Circuit with Microcontroller

Connect the ground end of the PIR Sensor to the GND pin of the micro:bit.

Connect the Signal pin of the PIR Sensor to pin PO of the micro:bit. This will be the digital pin that will send a signal of 1 when there is an object detected in the PIR Sensor's FOV. To connect PIR: > Connect the PIR Sensor Signal pin 1 to pin 0 of the micro:bit. 2 > Change the color of the wire to green. 3 PIR SENSOR REV B وزارة التعليم Ministry of Education 200 2024-1446 0:01 B 2 555-28027 1 2 3V GND PARALLAX 1 3 ↑ Figure 5.30: Connect the Signal pin of the PIR sensor +

2: Circuit with Microcontroller

Connect PIR:

Now that you have finished with the connections, you can add the following code and test it. Alarm System Figure 5.31: Test code 0: Simulator time: 00:04:48 2 6 7 B Saving... <7 Code Stop Simulation Send To 1 (micro:bit) pins.analog_set_pitch_pin (AnalogPin. P2) def on_forever () : if pins.digital_read_pin (DigitalPin.PO) basic.show leds (""" 10 11 BEEN 12 13 14 for index in range (2): pins.analog_pitch (500, 100) basic.pause (100) is basic. forever (on_forever) == 1: When you use the piezo buzzer in a project, make sure that the volume on your computer is high enough for you to hear the sound that the piezo buzzer produces. Piezo buzzer produces a sound two times at a frequency of 500 hertz, every 100 milliseconds (0.1 seconds). Display an exclamation mark when the PIR Sensor detects an object. No object has entered the PIR Sensor FOV. An object has entered the PIR Sensor FOV. Piezo buzzer doesn't produce a sound. PIE Sevice 10001 FARALLAX Figure 5.32: No object in PIR sensor FOVزارة التعليم Ministry of Education 2024-1446 Piezo buzzer produces a sound. PIR Sewace PARALLAX Figure 5.33: Object in PIR sensor FOV 201

2: Circuit with Microcontroller

Now that you have finished with the connections, you can add the following code and test it.

Exercises 1 Which component will you use to easily connect more electronic components? 2 Can you identify the problem with these connections? What do you need to do to correct it? 3 Why do we use the resistors in the following electronic circuit? 0 1 2 3V GND 12 a b c d e fghij 2 AC 3V GND a b c de fgh j 123456 7 9 10 10 10 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14 15 15 15 15 16 <-16 16 <-16 وزارة التعليم Ministry of Education 202 2024-1446

2: Circuit with Microcontroller

Which component will you use to easily connect more electronic components?

Can you identify the problem with these connections?

Why do we use the resistors in the following electronic circuit?

4 In the traffic light circuit in the lesson, we do not use the 3V pin of the micro:bit board. How do we power each LED? 5 What is the main difference between a distance sensor and a PIR sensor? Search on the Internet for applications of each electronic component. 6 How many electronic components can you connect to the ground pin of the micro:bit board? 7 In all micro:bit projects, you define a function named on_forever. Explain how you use it and why it is required. وزارة التعليم Ministry of Education 2024-1446 203

2: Circuit with Microcontroller

What is the main difference between a distance sensor and a PIR sensor?

How many electronic components can you connect to the ground pin of the micro:bit board?

In all micro:bit projects, you define a function named on_forever. Explain how you use it and why it is required.

in the traffic light circuit

8 Examine the following circuit and its code carefully and find the problem. What do you need to do to fix it? Alarm System وزارة التعليم Ministry of Education 2024-1446 Text All changes saved C Code Start Simulation Send To 土? 1 (micro:bit) alam-Q pins.analog set pitch pin (AnalogPin.P2) alarm-0 def on forever (1 global alam if pine.diaital read sin(imitalin. - ba bai 1 # Python code 2 # 3 alarm = 0 4 pins.analog_set_pitch_pin (AnalogPin. P2) 5 alarm = 0 6 7 def on_forever (): 8 global alarm PL 9 10 if pins.digital_read_pin (DigitalPin.PO) basic.show leds (""" == 0: 11 12 34 basic. Serial Mo 13 # 14 # # 15 16 "*") 17 else: 18 basic.show_leds (""" 19 20 21 22 23 24 25 26 27 28 29 30 31 32 else: 33 alarm = 10 pins.digital_write_pin (DigitalPin. P1, 1). for index in range (3): pins. analog_pitch (440, 200) basic.pause (100) if alarm > 0: alarm += -1 pins.digital_write_pin (Digital Pin. P1, 0) 34 basic.forever (on_forever)

2: Circuit with Microcontroller

Examine the following circuit and its code carefully and find the problem. What do you need to do to fix it?