Skip to content

Commit 2a4dc6a

Browse files
committed
Finish ultrasonic lesson base
1 parent 7beebbf commit 2a4dc6a

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

docs/week-5/ultrasonic-sensors.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,51 @@ Components required:
4242

4343
## Programming your ultrasonic sensor
4444

45+
To begin programming our ultrasonic sensor, we need to first include the ultrasonic sensor library.
46+
47+
```cpp
48+
#include "SR04.h"
49+
```
50+
51+
Then, instead of writing out the math which will compute the distance, we can simply call a function:
52+
53+
```cpp
54+
#define TRIG_PIN 12
55+
#define ECHO_PIN 11
56+
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
57+
long dist;
58+
```
59+
60+
Then, we can use the `sr04` object to calculate the distance and print it out to our serial monitor. The completed example sketch, therefore, is as follows:
61+
62+
```cpp
63+
#include "SR04.h"
64+
#define TRIG_PIN 12
65+
#define ECHO_PIN 11
66+
67+
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
68+
long dist;
69+
70+
void setup() {
71+
Serial.begin(9600);
72+
}
73+
74+
void loop() {
75+
dist = sr04.Distance(); // Sensor gets distance
76+
Serial.print(dist); // Prints distance
77+
Serial.println("cm");
78+
delay(750);
79+
}
80+
```
81+
4582
## Assignment
4683

4784
:::info Your Turn
48-
1.
85+
1. Create a circuit with your ultrasonic sensor and a buzzer to simulate a simple walking stick system for the visually impaired. If the distance is less than `20cm`, the buzzer will beep to warn of an obstacle. For a challenge, make the buzzer beep faster the closer you are to an object.
4986
:::
5087

5188
## Next Steps
5289

5390
This section includes links to help you dive deeper into the topics from this lesson. It's optional, so don't worry if you choose to skip it.
5491

55-
-
92+
- [Read this to understand more about how an ultrasonic sensor works.](https://maxbotix.com/blogs/blog/how-ultrasonic-sensors-work)

0 commit comments

Comments
 (0)