Skip to content

Commit 61b5afc

Browse files
move content from nested folder. add circuit 2B
1 parent 30ba633 commit 61b5afc

File tree

71 files changed

+194
-506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+194
-506
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f3918b49-9476-4415-a7e9-68539c202181",
6+
"metadata": {},
7+
"source": [
8+
"# Project 2 - Circuit 2B: Digital Trumpet\n",
9+
"\n",
10+
"Learn about digital inputs and buttons as you build your own digital trumpet!\n",
11+
"\n",
12+
"![What you need](images/sik-demo-prj2-cb-need.png)\n",
13+
"\n",
14+
"## New Components\n",
15+
"\n",
16+
"### Buttons\n",
17+
"\n",
18+
"[Buttons](https://learn.sparkfun.com/tutorials/button-and-switch-basics/all), also known as momentary switches, are switches that only remain in their on state as long as they’re being actuated, or pressed. Most often momentary switches are best used for intermittent user-input cases: reset button and keypad buttons. These switches have a nice, tactile, “clicky” feedback when you press them.\n",
19+
"\n",
20+
"![buttons](images/sik-docs-prj2-cb-button.jpg)\n",
21+
"\n",
22+
"Note that the different colors are just aesthetic. All of the buttons included behave the same no matter their color.\n",
23+
"\n",
24+
"## New Concepts\n",
25+
"\n",
26+
"### Binary Number System\n",
27+
"\n",
28+
"Number systems are the methods we use to represent numbers. We’ve all been mostly operating within the comfy confines of a base-10 number system, but there are many others. The base-2 system, otherwise known as [binary](https://learn.sparkfun.com/tutorials/binary), is common when dealing with computers and electronics. There are really only two ways to represent the state of anything: ON or OFF, HIGH or LOW, 1 or 0. And so, almost all electronics rely on a base-2 number system to store and manipulate numbers. The heavy reliance electronics places on binary numbers means it’s important to know how the base-2 number system works.\n",
29+
"\n",
30+
"### Digital Input\n",
31+
"\n",
32+
"In circuit 1A, you worked with digital outputs. This circuit focuses on [digital inputs](https://www.arduino.cc/en/Tutorial/DigitalPins). Digital inputs only care if something is in one of two states: TRUE or FALSE, HIGH or LOW, ON or OFF. Digital inputs are great for determining if a button has been pressed or if a switch has been flipped.\n",
33+
"\n",
34+
"### Pull-up Resistors\n",
35+
"\n",
36+
"A [pull-up resistor](https://learn.sparkfun.com/tutorials/pull-up-resistors) is a small circuit that holds the voltage HIGH (5V) on a pin until a button is pressed, pulling the voltage LOW (0V). The most common place you will see a pull-up resistor is when working with buttons. A pull-up resistor keeps the button in one state until it is pressed. The RedBoard has built-in pull-up resistors, but they can also be added to a circuit externally. This circuit uses the internal pull-up resistors, covered in more detail in the Code to Note section.\n",
37+
"\n",
38+
"## Hardware Hookup\n",
39+
"\n",
40+
"Buttons are not polarized. However, they do merit a closer look. Buttons make momentary contact from one connection to another, so why are there four legs on each button? The answer is to provide more stability and support to the buttons in your breadboard circuit. Each row of legs is connected internally. When the button is pressed, one row connects to the other, making a connection between all four pins.\n",
41+
"\n",
42+
"The buzzer is polarized. To see which leg is positive and which is negative, flip the buzzer over and look at the markings underneath. Keep track of which pin is where, as they will be hard to see once inserted into the breadboard. There is also text on the positive side of the buzzer, along with a tiny (+) symbol.\n",
43+
"\n",
44+
"![ButtonConnections](images/sik-demo-prj2-cb-button-cons.jpg)\n",
45+
"\n",
46+
"If the button's legs don't line up with the slots on the breadboard, rotate it 90 degrees.\n",
47+
"\n",
48+
"Ready to start hooking everything up? Check out the circuit diagram and hookup table below to see how everything is connected.\n",
49+
"\n",
50+
"### Circuit Diagram\n",
51+
"\n",
52+
"TODO: Replace with RP2350...\n",
53+
"\n",
54+
"![Hookup](images/sik-docs-prj2-cb-hookup.jpg)\n",
55+
"\n",
56+
"**Note for Advanced Users**: If you know how to read datasheets and schematics, you can also refer to the schematic below as an alternative.\n",
57+
"\n",
58+
"![Schematic](images/sik-docs-prj2-cb-schem.jpg)\n",
59+
"\n",
60+
"## Hookup Table\n",
61+
"\n",
62+
"| Component | RedBoard | Breadboard | Breadboard | Breadboard |\n",
63+
"|------------------|------------------|-----------------|-----------------|-----------------|\n",
64+
"| Buzzer | J1 (Buzzer +) | J3 (Buzzer -) | | |\n",
65+
"| Potentiometer | | B1 | B2 | B3 |\n",
66+
"| Jumper Wire | GND | GND Rail ( - ) | | |\n",
67+
"| Jumper Wire | Digital Pin 34 | F1 | | |\n",
68+
"| Jumper Wire | | E2 | GND Rail ( - ) | |\n",
69+
"| Jumper Wire | | E1 | F3 | |\n",
70+
"| Push Button | D16/D18 | G16/G18 | | |\n",
71+
"| Push Button | D22/D24 | G22/G24 | | |\n",
72+
"| Push Button | D28/D30 | G28/G30 | | |\n",
73+
"| Jumper Wire | Digital Pin 33 | J18 | | |\n",
74+
"| Jumper Wire | Digital Pin 32 | J24 | | |\n",
75+
"| Jumper Wire | Digital Pin 31 | J30 | | |\n",
76+
"| Jumper Wire | | J16 | GND Rail ( - ) | |\n",
77+
"| Jumper Wire | | J22 | GND Rail ( - ) | |\n",
78+
"| Jumper Wire | | J28 | GND Rail ( - ) | |\n"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"id": "1bd52930-d783-460a-923b-41a3c37d2f2b",
84+
"metadata": {},
85+
"source": [
86+
"## Using Buttons with the Buzzer\n",
87+
"\n",
88+
"Now that your circuit is built, it's time to use the buzzer. This is done using MicroPython, which is running on the RedBoard.\n",
89+
"\n",
90+
"The first step is to connect your RedBoard to a USB port on this computer.\n",
91+
"\n",
92+
"Select the \"Connect\" button at the bottom right of this screen and a panel is displayed\n",
93+
"\n",
94+
"Select the \"Connect Device\" Button, and when the selection dialog appears, select the port with that displays ***Board in FS mode (...)*** or *TBD*\n",
95+
"\n",
96+
"![Select a Port](images/sik-demo-select-port.png)\n",
97+
"\n",
98+
"With the RedBoard connected, use the following MicroPython commands to move the servo. \n",
99+
"\n",
100+
"### Using MicroPython\n",
101+
"\n",
102+
"The following MicroPython commands are entered to operate the buzzer. \n",
103+
"\n",
104+
"**REMEMBER** To enter a MicroPython command, hold down either the Control (on Windows) or Command (on Mac) key when pressing *Enter*\n",
105+
"\n",
106+
"#### Step 1 - Setup\n",
107+
"\n",
108+
"Lets start by importing any of the libaries we plan on using and setting up our pins."
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"id": "aafcc22d",
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"from machine import Pin # Allows us to use \"Pin\" to use code to interface with the pins on our board\n",
119+
"from machine import PWM # Allows us to use \"PWM\" (pulse-width modulation) to control the brightness of our LED\n",
120+
"\n",
121+
"firstKeyPin = Pin(33, Pin.IN, Pin.PULL_UP) # Create a Pin object for the first key on pin 33, set as input with pull-up resistor\n",
122+
"secondKeyPin = Pin(32, Pin.IN, Pin.PULL_UP) # Create a Pin object for the second key on pin 32, set as input with pull-up resistor\n",
123+
"thirdKeyPin = Pin(31, Pin.IN, Pin.PULL_UP) # Create a Pin object for the third key on pin 35, set as input with pull-up resistor\n",
124+
"\n",
125+
"pwmSpeaker = PWM(Pin(34), freq=0, duty_u16=0) # Create a PWM object on pin 34 with a frequency of 0Hz and an initial \"on time\" of 0 (off)"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"id": "b1545bf1",
131+
"metadata": {},
132+
"source": [
133+
"#### Step 2 - Reading Buttons to Decide Buzzer Frequency\n",
134+
"Now for each button, let's check if the button is pressed or not and play a different note for each!\n",
135+
"\n",
136+
"To read our digital inputs, we'll check whether they are high (1) or low (0) with their built in `value()` function."
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"id": "b9f2133d",
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"# In an infinite loop, we will check the state of the keys and play a note when a key is pressed\n",
147+
"while True:\n",
148+
" if firstKeyPin.value() == 0: # If the first key is pressed (value is low)\n",
149+
" pwmSpeaker.freq(262) # Play the note C (262Hz)\n",
150+
" pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)\n",
151+
" elif secondKeyPin.value() == 0: # If the second key is pressed (value is low)\n",
152+
" pwmSpeaker.freq(330) # Play the note E (330Hz)\n",
153+
" pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)\n",
154+
" elif thirdKeyPin.value() == 0: # If the third key is pressed (value is low)\n",
155+
" pwmSpeaker.freq(392) # Play the note G (392Hz)\n",
156+
" pwmSpeaker.duty_u16(32768) # Set the duty cycle to 50% (half as loud as possible)\n",
157+
" else: # If no key is pressed\n",
158+
" pwmSpeaker.freq(0)\n",
159+
" pwmSpeaker.duty_u16(0) # turn off the speaker "
160+
]
161+
},
162+
{
163+
"cell_type": "markdown",
164+
"id": "10d5c3fe-852f-42f7-8378-85dc8c8c7c3e",
165+
"metadata": {},
166+
"source": [
167+
"## What You Should See\n",
168+
"Different tones will play when you press different keys. Turning the potentiometer will adjust the volume.\n",
169+
"\n",
170+
"## You've Completed Circuit 2B!\n",
171+
"\n",
172+
"Continue to circuit 2C to learn about for loops, measuring durations, and put together all you've learned in project 2.\n",
173+
"\n",
174+
"![Next - Circuit B](images/sik-demo-prj3-cb-next.png)"
175+
]
176+
}
177+
],
178+
"metadata": {
179+
"kernelspec": {
180+
"display_name": "MicroPython upydevice kernel",
181+
"language": "python",
182+
"name": "micropython-upydevice"
183+
},
184+
"language_info": {
185+
"codemirror_mode": "python",
186+
"file_extension": ".py",
187+
"mimetype": "text/x-python",
188+
"name": "python",
189+
"pygments_lexer": "python"
190+
}
191+
},
192+
"nbformat": 4,
193+
"nbformat_minor": 5
194+
}

0 commit comments

Comments
 (0)