Skip to content

Commit 2c31400

Browse files
Fix circuit 3B
1 parent e887d9b commit 2c31400

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

content/content/SIK_Project3_CircuitB_Full.ipynb

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@
143143
"| 330Ω Resistor (orange, orange, brown) | | E23 | F23 | | |\n",
144144
"| 330Ω Resistor (orange, orange, brown) | | E25 | F25 | | |\n",
145145
"| Jumper Wire | | E24 | GND Rail (-) | | | \n",
146-
"| Jumper Wire | Digital pin TODO | J25 | | | |\n",
147-
"| Jumper Wire | Digital pin TODO | J23 | | | |\n",
148-
"| Jumper Wire | Digital pin TODO | J22 | | | |\n",
146+
"| Jumper Wire | Digital pin 28 | J25 | | | |\n",
147+
"| Jumper Wire | Digital pin 30 | J23 | | | |\n",
148+
"| Jumper Wire | Digital pin 32 | J22 | | | |\n",
149149
"| Distance Sensor | | A3 (Vcc) | A4 (Trig) | A5 (Echo) | A6 (GND) |\n",
150-
"| Jumper Wire | Digital Pin TODO | E4 (Trig) | | | |\n",
151-
"| Jumper Wire | Digital Pin TODO | E5 (Echo) | | | |\n",
150+
"| Jumper Wire | Digital Pin 20 | E4 (Trig) | | | |\n",
151+
"| Jumper Wire | Digital Pin 21 | E5 (Echo) | | | |\n",
152152
"| Jumper Wire | | E3 | 5V Rail (+) | | | \n",
153153
"| Jumper Wire | | E6 | GND Rail (-) | | |\n",
154154
"\n",
@@ -208,6 +208,7 @@
208208
"id": "52a909f5",
209209
"metadata": {},
210210
"source": [
211+
"#### Step 2 - Measuring Distance\n",
211212
"Now let's create a function for measuring a \"pulse\" or how long a pin is high. We can use the built in `time.ticks_us()` method to get the current time. We can use this to see how long the echo pin is high later to calculate the distance with our distance sensor. TODO: Is this to fancy for users at this point? should we instead have this be in some utility library"
212213
]
213214
},
@@ -243,7 +244,7 @@
243244
"id": "b1545bf1",
244245
"metadata": {},
245246
"source": [
246-
"Next, let's create a function to calculate distance. We'll send out an ultrasonic pulse on the trigPin and then measure how long it takes for the pulse to bounce back to the sensor by using our measure_pulse function on the echo pin."
247+
"Next, let's create a function to calculate distance. We'll send out an ultrasonic pulse on the trigPin and then measure how long it takes for the pulse to bounce back to the sensor by using the built-in \"time_pulse_us\" function on the echo pin."
247248
]
248249
},
249250
{
@@ -254,44 +255,27 @@
254255
"outputs": [],
255256
"source": [
256257
"from time import sleep_us\n",
258+
"from machine import time_pulse_us\n",
257259
"\n",
258260
"def get_distance():\n",
259261
" trigPin.high()\n",
260-
" sleep_us(10) # Send a 10 microsecond pulse to the trigger pin to start the measurement\n",
262+
" sleep_us(10) # Send at least a 10 microsecond pulse to the trigger pin to start the measurement\n",
261263
" trigPin.low() # Set the trigger pin low to stop the measurement\n",
262264
"\n",
263-
" echoTime = measure_pulse(echoPin) # Measure the duration of the echo pulse\n",
265+
" echoTime = time_pulse_us(echoPin, 1)\n",
264266
"\n",
265267
" calculatedDistance = echoTime / 148.0 #calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)\n",
266268
"\n",
267269
" return calculatedDistance # Return the calculated distance\n"
268270
]
269271
},
270-
{
271-
"cell_type": "code",
272-
"execution_count": null,
273-
"id": "4494b462-505b-4a23-8ebc-28b54152bd88",
274-
"metadata": {},
275-
"outputs": [],
276-
"source": [
277-
"from machine import Pin # Allows us to use \"Pin\" to use code to interface with the pins on our board\n",
278-
"from machine import ADC # Allows us to use \"ADC\" (analog-to-digital conversion) to read from our analog pin\n",
279-
"# Possibly from servo import Servo # Import the Servo class (TODO: If we have this defined elsewhere we need to import it)\n",
280-
"\n",
281-
"led_pin = Pin(34, Pin.OUT) # Create a pin variable for the led pin (pin 34)\n",
282-
"potentiometer = ADC(Pin.board.A0) # Create an ADC variable for reading the potentiometer value from analog pin A0\n",
283-
"\n",
284-
"# Create a Servo object on pin 28 (this is the pin we will use to control the servo motor)\n",
285-
"myServo = Servo(pin_id=28)"
286-
]
287-
},
288272
{
289273
"cell_type": "markdown",
290274
"id": "2d94be96-a8c9-4932-8111-58d10a8622a9",
291275
"metadata": {},
292276
"source": [
293-
"#### Step 2 - Writing to the Servo\n",
294-
"Now let's create a loop where we read the potentiometer value and move the servo based on the potentiometer position."
277+
"#### Step 3 - Putting it all together\n",
278+
"Now let's put the functions we created together along with some LED control. Let's create a loop where we read the distance from the ultrasonic sensor and update the LED color depending on how far objects are from the sensor."
295279
]
296280
},
297281
{
@@ -301,12 +285,32 @@
301285
"metadata": {},
302286
"outputs": [],
303287
"source": [
304-
"# Infinite loop to read the potentiometer value and control the servo motor\n",
288+
"from time import sleep\n",
289+
"\n",
290+
"# Infinite loop to read the distance sensor and update the RGB LED colors based on the distance\n",
305291
"while True:\n",
306-
" pot_value = potentiometer.read_u16() # Read the potentiometer value (0-65535)\n",
307-
" angle = (pot_value / 65535) * 180 # Convert the value to an angle (0-180 degrees)\n",
292+
" distance = get_distance() # Get the distance from the sensor\n",
293+
" print(f\"Distance (in): {distance: 5}\", end='\\r') # Print our readings (don't mind the fanciness of this line it just makes the print format nicely)\n",
294+
"\n",
295+
" if distance <= 10:\n",
296+
" # Make the RGB LED red \n",
297+
" pwmRed.duty_u16(65535)\n",
298+
" pwmGreen.duty_u16(0)\n",
299+
" pwmBlue.duty_u16(0)\n",
300+
"\n",
301+
" elif distance < 20:\n",
302+
" # Make the RGB LED yellow\n",
303+
" pwmRed.duty_u16(65535)\n",
304+
" pwmGreen.duty_u16(32768)\n",
305+
" pwmBlue.duty_u16(0)\n",
308306
" \n",
309-
" myServo.write(angle) # Write the angle to the servo motor"
307+
" else:\n",
308+
" # Make the RGB LED green\n",
309+
" pwmRed.duty_u16(0)\n",
310+
" pwmGreen.duty_u16(65535)\n",
311+
" pwmBlue.duty_u16(0)\n",
312+
"\n",
313+
" sleep(0.050) # Sleep for 50 ms between each reading"
310314
]
311315
},
312316
{
@@ -315,13 +319,13 @@
315319
"metadata": {},
316320
"source": [
317321
"## What You Should See\n",
318-
"Turning the potentiometer will cause the servo to turn.\n",
322+
"Move your hand or a large, flat object closer and farther away from the distance sensor. As the object approaches, the light will change from green to yellow to red.\n",
319323
"\n",
320-
"## You've Completed Circuit 3A!\n",
324+
"## You've Completed Circuit 3B!\n",
321325
"\n",
322326
"Continue to circuit 3B to learn about distance sensors.\n",
323327
"\n",
324-
"![Next - Circuit B](images/sik-demo-prj3-ca-next.png)"
328+
"![Next - Circuit B](images/sik-demo-prj3-cb-next.png)"
325329
]
326330
}
327331
],
20.2 KB
Loading

0 commit comments

Comments
 (0)