|
38 | 38 | "source": [ |
39 | 39 | "### Background \n", |
40 | 40 | "\n", |
41 | | - "(Task_B1)=\n", |
| 41 | + "(Task_B1_2_1)=\n", |
42 | 42 | "#### Task B1: What is Whack-a-mole?\n", |
43 | 43 | "Whack-a-mole is a popular arcade game in which the player uses a mallet/hammer to hit moles as they pop up from random holes. The player is scored based on their reaction time/how quickly they are able to hit the moles. \n", |
44 | 44 | "\n", |
45 | | - "During this practical, you will be creating your own version of this game on the ALPACA. As the ALPACA does not have moles or a mallet, you will replace the moles with LEDs and the mallet with buttons. You will programme the LEDs to turn on at random and will use the buttons to turn the LEDs off, analogous to how the mallet is used to hit the moles in the arcade game. \n", |
| 45 | + "During this practical, you will be creating your own version of this game on the ALPACA. As the ALPACA has neither moles nor a mallet, you will replace the moles with LEDs and the mallet with buttons. You will programme the LEDs to turn on at random and will use the buttons to turn the LEDs off, analogous to how the mallet is used to hit the moles in the arcade game. You will also measure the reaction time of the player.\n", |
46 | 46 | "\n", |
47 | | - "(Task_B2)=\n", |
| 47 | + "(Task_B2_2_1)=\n", |
48 | 48 | "#### Task B2: Light emitting diodes (LEDs)\n", |
49 | 49 | "A light emitting diode (LED) is a type of directional component that converts electricity into light. You will dive deeper into the concept of diodes and LEDs in future practicals, but for today, it is only important that you understand how to connect an LED. As seen in the figure below, to conduct electricity and emit light, the long leg of the LED (also referred to as the anode), should be connected to a higher potential than the short leg (also referred to as the cathode).\n", |
50 | 50 | "\n", |
|
65 | 65 | "Schematic of LEDs on the ALPACA, indicating the anode and cathode\n", |
66 | 66 | "```\n", |
67 | 67 | "\n", |
68 | | - "(Task_B3)=\n", |
| 68 | + "(Task_B3_2_1)=\n", |
69 | 69 | "#### Task B3: Digital input and output pins\n", |
70 | | - "The ALPACA contains both digital input and digital output pins. These pins are referred to as *digital*, as they are only able to be in one of two states, either HIGH (+5 V) or LOW (0 V). This is in contrast to analogue pins, which can read or output a continuous range of voltages. \n", |
| 70 | + "The ALPACA contains both digital input and digital output pins. These pins are referred to as *digital*, as they are only able to be in one of two states, either high (5 V) or low (0 V). This is in contrast to analogue pins, which can read or output a continuous range of voltages. \n", |
71 | 71 | "\n", |
72 | | - "Digital output pins output +5 V when set to True and output 0 V when set to False. Similarly, a digital input pin can determine whether +5 V or 0 V are connected. When +5 V are connected to a digital input pin, it will be assigned as True and when 0 V are connected, it will be assigned as False. \n", |
| 72 | + "Digital output pins output 5 V when set to True and output 0 V when set to False. Similarly, a digital input pin can determine whether 5 V or 0 V are connected. When 5 V are connected to a digital input pin, it will be assigned as True and when 0 V are connected, it will be assigned as False. \n", |
73 | 73 | "\n", |
74 | | - "To programme a digital input or output pin, you first need to import the Pin function from the machine module. This allows you to define pins. You then need to define a pin using the pin ID and state whether the pin is an output or input pin. Once a digital output pin is defined, it can be programmed as True to output +5 V, or as false to output 0 V and once a digital input pin is defined, it is possible to check whether it is connected to + 5V or 0 V. \n", |
| 74 | + "To programme a digital input or output pin, you first need to import the Pin function from the machine module MicroPython. This allows you to define pins. You then need to define a pin using the pin ID and state whether the pin is an output or input pin. \n", |
75 | 75 | "\n", |
| 76 | + "```{tip}\n", |
| 77 | + "Pin IDs digital output pins:\n", |
| 78 | + "- 10\n", |
| 79 | + "- 11\n", |
| 80 | + "- 12\n", |
| 81 | + "- 13\n", |
76 | 82 | "\n", |
77 | | - "For example, pin 14 on the Pico-Pi is a digital output pin and pin 19 is a digital input pin. In the code cell below, you can see how these pins can be defined and programmed. \n", |
| 83 | + "Pin IDs digital input pins:\n", |
| 84 | + "- 14\n", |
| 85 | + "- 15\n", |
| 86 | + "- 16\n", |
| 87 | + "- 17 \n", |
| 88 | + "\n", |
| 89 | + "You can also find the pin IDs in the ALPACA manual.\n", |
| 90 | + "```\n", |
| 91 | + "\n", |
| 92 | + "Once a digital output pin is defined, it can be programmed as True to output 5 V, or as false to output 0 V and once a digital input pin is defined, it is possible to check whether it is connected to 5V or 0 V. \n", |
| 93 | + "\n", |
| 94 | + "\n", |
| 95 | + "For example, pin 10 on the Pico-Pi is a digital output pin and pin 14 is a digital input pin. In the code cell below, you can see how these pins can be defined and programmed. \n", |
78 | 96 | "\n", |
79 | 97 | "```{note}\n", |
80 | 98 | "The names Dout1 and Din1 were selected, as they are easy to remember. You can assign different names to the pins. \n", |
|
90 | 108 | "source": [ |
91 | 109 | "from machine import Pin #Import the module used to define pins\n", |
92 | 110 | "\n", |
93 | | - "Dout1 = Pin(14, Pin.OUT) #Give the pin a name. Define the pin using the pin ID (in this case, the ID is 14). State whether it is an input/output pin\n", |
94 | | - "Din1 = Pin(19, Pin.IN) #Give the pin a name. Define the pin using the pin ID (in this case, the ID is 19). State whether it is an input/output pin\n", |
| 111 | + "Dout1 = Pin(10, Pin.OUT) #Give the pin a name. Define the pin using the pin ID (in this case, the ID is 10). State whether it is an input/output pin\n", |
| 112 | + "Din1 = Pin(14, Pin.IN) #Give the pin a name. Define the pin using the pin ID (in this case, the ID is 14). State whether it is an input/output pin\n", |
95 | 113 | "\n", |
96 | | - "Dout1.value(True) #Pin 14 will output +5 V\n", |
97 | | - "Dout1.value(False) #Pin 14 will output 0 V\n", |
| 114 | + "Dout1.value(True) #The value of pin 10 is assigned as True, so it will output 5 V\n", |
| 115 | + "Dout1.value(False) #The value of pin 10 is assigned as False, so it will output 0 V\n", |
98 | 116 | "\n", |
99 | | - "isPressed = Din1.value() #The variable isPressed will be True when + 5V is connected to pin 19 and False when 0 V are connected to pin 19" |
| 117 | + "isPressed = Din1.value() #The variable isPressed will be True when 5V is connected to pin 10 and False when 0 V are connected to pin 14" |
100 | 118 | ] |
101 | 119 | }, |
102 | 120 | { |
103 | 121 | "cell_type": "markdown", |
104 | 122 | "id": "4629d3db", |
105 | 123 | "metadata": {}, |
106 | 124 | "source": [ |
107 | | - "(Task_B4)=\n", |
| 125 | + "(Task_B4_2_1)=\n", |
108 | 126 | "#### Task B4: Switches and pull-up/pull-down resistors\n", |
109 | 127 | "Analogous to the mallet which is used to hit the moles in the Whack-a-mole game, you will use switches to turn off the LEDs. A switch is a valuable electronic component which can be used to close or open a path for current flow. A button is a special type of switch that works when pushed. \n", |
110 | 128 | "\n", |
|
120 | 138 | "As you can see in this figure, the circuit containing the button also contains a 10 kΩ resistor. \n", |
121 | 139 | "\n", |
122 | 140 | "We have two different options for how we can connect the button with respect to the resistor. \n", |
123 | | - "\n" |
| 141 | + "\n", |
| 142 | + "In the figure shown below, the resistor is connected in such a way that we refer to it as a **pull-up resistor**.\n", |
| 143 | + "\n", |
| 144 | + "```{figure} images/Pull_up_resistor.svg\n", |
| 145 | + "---\n", |
| 146 | + "height: 200px\n", |
| 147 | + "name: Pull up resistor\n", |
| 148 | + "---\n", |
| 149 | + "Pull-up resistor configuration\n", |
| 150 | + "```\n", |
| 151 | + "In this configuration, when the button is not pressed, the digital input pin will be connected to 5 V. This means that the value of the pin will be assigned as True by default. When the button is pressed, the digital input pin will be connected to ground (0 V). The 10 kΩ resistor is connected between the 5 V supply and ground to limit the flow of current, preventing a short circuit when the button is pressed.\n", |
| 152 | + "\n", |
| 153 | + "We can also connect the resistor as a **pull-down resistor**, as seen in the figure below.\n", |
| 154 | + "\n", |
| 155 | + "```{figure} images/Pull_down_resistor.svg\n", |
| 156 | + "---\n", |
| 157 | + "height: 200px\n", |
| 158 | + "name: Pull down resistor\n", |
| 159 | + "---\n", |
| 160 | + "Pull-down resistor configuration\n", |
| 161 | + "```\n", |
| 162 | + "In this configuration, when the button is not pressed, the digital input pin will be connected to ground (0 V). This means that the value of the pin will be assigned as False by default. When the button is pressed, the digital input pin will be connected to 5 V. The 10 kΩ resistor is connected between the 5 V supply and ground to limit current flow when the button is pressed, preventing a short circuit. \n", |
| 163 | + "\n", |
| 164 | + "```{hint}\n", |
| 165 | + "In summary:\n", |
| 166 | + "- Pull-up resistor configuration: Default value of the digital input pin (when the button is not pressed) is 5 V or True. \n", |
| 167 | + "- Pull-down resistor configuration: Default value of the digital input pin (when the button is not pressed) is 0 V or False.\n", |
| 168 | + "- In both configurations, the 10 kΩ resistor limits the flow of current when the button is pressed, preventing a short circuit.\n", |
| 169 | + "```" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "id": "a9938ab6", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "### Anticipate \n", |
| 178 | + "\n", |
| 179 | + "(Task_A1_2_1)=\n", |
| 180 | + "#### Task A1: Connecting an LED to a switch in pull-up vs pull-down configuration\n", |
| 181 | + "\n", |
| 182 | + "Instead of connecting the output of a switch to a digital input pin, you can also connect it to an LED. This way, you can control the behaviour of the LED by pushing the button. \n", |
| 183 | + "\n", |
| 184 | + "We will begin by examining the effect of connecting the LED to a button in a pull-up configuration. \n", |
| 185 | + "\n", |
| 186 | + "Think back to [Task B4](Task_B4_2_1). What is the default output (when the button is not pressed) of a button in a pull-up configuration? What is the output when a button in a pull-up configuration is pressed?\n", |
| 187 | + "\n", |
| 188 | + "In the figure below, a button with a pull-up resistor is connected to an LED in two different configurations (A and B).\n", |
| 189 | + "\n", |
| 190 | + "```{figure} images/Pull_up_switch_LED_config.svg\n", |
| 191 | + "---\n", |
| 192 | + "width: 500px\n", |
| 193 | + "name: Pull-up button connected to LED in two configurations\n", |
| 194 | + "---\n", |
| 195 | + "Schematic of a button with a pull-up resistor connected to an LED in two configurations\n", |
| 196 | + "```\n", |
| 197 | + "Consider [Task B2](Task_B2_2_1), for configuration A and configuration B, predict what the LED will do when the button is not pressed and when the button is pressed. \n", |
| 198 | + "\n", |
| 199 | + "Recall [Task B4](Task_B4_2_1). What is the default output (when the button is not pressed) of a button in a pull-down configuration? What is the output when a button in a pull-down configuration is pressed?\n", |
| 200 | + "\n", |
| 201 | + "In the figure below, a button with a pull-down resistor is connected to an LED in two different configurations (C and D).\n", |
| 202 | + "\n", |
| 203 | + "```{figure} images/Pull_down_switch_config.svg\n", |
| 204 | + "---\n", |
| 205 | + "width: 500px\n", |
| 206 | + "name: Pull-down button connected to LED in two configurations\n", |
| 207 | + "---\n", |
| 208 | + "Schematic of a button with a pull-down resistor connected to an LED in two configurations\n", |
| 209 | + "```\n", |
| 210 | + "\n", |
| 211 | + "Consider [Task B2](Task_B2_2_1), for configuration A and configuration B, predict what the LED will do when the button is unpressed and when the button is pressed. \n", |
| 212 | + "\n", |
| 213 | + "\n", |
| 214 | + "\n", |
| 215 | + "-----\n", |
| 216 | + "\n", |
| 217 | + "Compare connecting LED to switch in pull-up and pull-down config - validate in I&I1\n", |
| 218 | + "Idea is that they understand when output is true and when output is false\n", |
| 219 | + "\n", |
| 220 | + "Explain that can't implement logic with this yet \n", |
| 221 | + "\n", |
| 222 | + "Look at connecting switch to digital input pin and connecting digital output pin to the LED\n", |
| 223 | + "Explain that this then allows us to do logic: by setting hte output pin to true, we can turn on the LED. When we push the button, we will get a true/false signal (depending on whether have pull-up or pull-down config). Based on this, when get certain signal at input pin, can output something in response from output pin to make LED respond to whatever is happening with the switch " |
124 | 224 | ] |
125 | 225 | } |
126 | 226 | ], |
|
0 commit comments