Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplFerris committed Dec 28, 2024
1 parent 960ed8e commit bf902d2
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 160 deletions.
5 changes: 3 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
- [Dim LED with RP HAL](./led/rp-hal.md)
- [Using External LED](./led/external-led.md)
- [Ultrasonic](./ultrasonic/index.md)
- [How it works?](./ultrasonic/concepts.md)
- [Light it Up](./ultrasonic/action.md)
- [How it works?](./ultrasonic/how-it-works.md)
- [Circuit](./ultrasonic/circuit.md)
- [Code](./ultrasonic/code.md)
- [OLED Display](./oled/index.md)
- [Hello World](./oled/hello-oled.md)
- [Ferris](./oled/ferris.md)
Expand Down
111 changes: 111 additions & 0 deletions src/ultrasonic/circuit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Circuit

Connection for the Pico and Ultrasonic:
<table>
<thead>
<tr>
<th>Pico Pin</th>
<th style="height: 4px; width: 250px; margin: 0 auto;">Wire</th>
<th>HC-SR04 Pin</th>
</tr>
</thead>
<tbody>
<tr>
<td>3.3V</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire red" style="height: 4px; width: 200px; margin: 0 auto;">
<div class="male-left"></div>
<div class="male-right"></div>
</div>
</td>
<td>VCC</td>
</tr>
<tr>
<td>GPIO 17</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire green" style="height: 4px; width: 200px; margin: 0 auto;">
<div class="male-left"></div>
<div class="male-right"></div>
</div>
</td>
<td>Trig</td>
</tr>
<tr>
<td>GPIO 16</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire yellow" style="height: 4px; width: 200px; margin: 0 auto;">
<div class="male-left"></div>
<div class="male-right"></div>
</div>
</td>
<td>Echo</td>
</tr>
<tr>
<td>GND</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire black" style="height: 4px; width: 200px; margin: 0 auto;">
<div class="male-left"></div>
<div class="male-right"></div>
</div>
</td>
<td>GND</td>
</tr>
</tbody>
</table>

- **VCC**: Connect the VCC pin on the HC-SR04 to the 3.3V pin on the Pico.
- Although the HC-SR04 generally operates at 5V, using 3.3V helps protect the Pico since the Pico GPIO pins are only 3.3V tolreant.
- Other considerations: Alternatively, you can use HCSR04+ (which can operate at both 3.3v and 5V) or use voltage divider.
- **Trig**: Connect to GPIO 17 on the Pico to start the ultrasonic sound pulses.
- **Echo**: Connect to GPIO 16 on the Pico; this pin sends a pulse when it detects the reflected signal, and the pulse length shows how long the signal took to return.
- **GND**: Connect to the ground pin on the Pico.
- **LED**: Connect the anode (long leg) of the LED to GPIO 3, as in the [External LED setup](../blinky/external-led.md).



Connection for the Pico and LED:

<table>
<thead>
<tr>
<th>Pico Pin</th>
<th style="width: 250px; margin: 0 auto;">Wire</th>
<th>Component</th>
</tr>
</thead>
<tbody>
<tr>
<td>GPIO 3</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire orange" style="width: 200px; margin: 0 auto;">
<div class="female-left"></div>
<div class="female-right"></div>
</div>
</td>
<td>Resistor</td>
</tr>
<tr>
<td>Resistor</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire orange" style="width: 200px; margin: 0 auto;">
<div class="female-left"></div>
<div class="female-right"></div>
</div>
</td>
<td>Anode (long leg) of LED</td>
</tr>
<tr>
<td>GND</td>
<td style="text-align: center; vertical-align: middle; padding: 0;">
<div class="wire black" style="width: 200px; margin: 0 auto;">
<div class="female-left"></div>
<div class="female-right"></div>
</div>
</td>
<td>Cathode (short leg) of LED</td>
</tr>
</tbody>
</table>


<a href="./images/pico-ultrasonic-led.jpg"><img style="display: block; margin: auto;" alt="pico2" src="./images/pico-ultrasonic-led.jpg"/></a>
17 changes: 10 additions & 7 deletions src/ultrasonic/action.md → src/ultrasonic/code.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Action
## Writing Rust Code Use HC-SR04 Ultrasonic Sensor with Pico 2

We'll start by generating the project using the template, then modify the code to fit the current project's requirements.

Expand Down Expand Up @@ -65,7 +65,7 @@ trigger.set_low().ok().unwrap();
```

### Step 2: Measure the Echo Time
Now, measure the time the Echo pin remains high, which represents the round-trip time of the sound wave.
Next, we will use two loops. The first loop will run as long as the echo pin state is LOW. Once it goes HIGH, we will record the current time in a variable. Then, we start the second loop, which will continue as long as the echo pin remains HIGH. When it returns to LOW, we will record the current time in another variable. The difference between these two times gives us the pulse width.

```rust
let mut time_low = 0;
Expand All @@ -87,14 +87,18 @@ let time_passed = time_high - time_low;
```

### Step 3: Calculate Distance
Using the measured time, calculate the distance to the object. The speed of sound in air is approximately 0.0343 cm/µs.
To calculate the distance, we need to use the pulse width. The pulse width tells us how long it took for the ultrasonic waves to travel to an obstacle and return. Since the pulse represents the round-trip time, we divide it by 2 to account for the journey to the obstacle and back.

The speed of sound in air is approximately 0.0343 cm per microsecond. By multiplying the time (in microseconds) by this value and dividing by 2, we obtain the distance to the obstacle in centimeters.

```rust
let distance = time_passed as f64 * 0.0343 / 2.0;
```

### Step 3: Calculate Distance
Finally, adjust the LED brightness based on the distance. If the distance is below a certain threshold (e.g., 30 cm), increase the brightness proportionally; otherwise, turn off the LED.
### Step 4: PWM Duty cycle for LED
Finally, we adjust the LED brightness based on the measured distance.

The duty cycle percentage is calculated using our own logic, you can modify it to suit your needs. When the object is closer than 30 cm, the LED brightness will increase. The closer the object is to the ultrasonic module, the higher the calculated ratio will be, which in turn adjusts the duty cycle. This results in the LED brightness gradually increasing as the object approaches the sensor.

```rust
let duty_cycle = if distance < 30.0 {
Expand All @@ -104,9 +108,8 @@ let duty_cycle = if distance < 30.0 {
0
};

// Set the LED brightness
// Change the LED brightness
led.set_duty_cycle(duty_cycle).unwrap();

```

### Complete Logic of the loop
Expand Down
34 changes: 0 additions & 34 deletions src/ultrasonic/concepts.md

This file was deleted.

80 changes: 80 additions & 0 deletions src/ultrasonic/how-it-works.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# How Does an Ultrasonic Sensor Work?

Ultrasonic sensors work by emitting sound waves at a frequency too high(40kHz) for humans to hear. These sound waves travel through the air and bounce back when they hit an object. The sensor calculates the distance by measuring how long it takes for the sound waves to return.

<img style="display: block; margin: auto;width:500px" alt="ultrasonic" src="./images/ultrasonic.jpg"/>

- **Transmitter:** Sends out ultrasonic sound waves.
- **Receiver:** Detects the sound waves that bounce back from an object.

**Formula to calculate distance**:
```
Distance = (Time x Speed of Sound) / 2
```

The speed of sound is approximately 0.0343 cm/µs (or 343 m/s) at normal air pressure and a temperature of 20°C.

## Example Calculation:

Let's say the ultrasonic sensor detects that the sound wave took 2000 µs to return after hitting an object.

Step 1: Calculate the total distance traveled by the sound wave:
```
Total distance = Time x Speed of Sound
Total distance = 2000 µs x 0343 cm/µs = 68.6 cm
```

Step 2: Since the sound wave traveled to the object and back, the distance to the object is half of the total distance:
```
Distance to object = 68.6 cm / 2 = 34.3 cm
```

Thus, the object is 34.3 cm away from the sensor.


## HC-SR04 Pinout
The module has four pins: VCC, Trig, Echo, and GND.

<table style="width:300px;height:200px;">
<tr>
<th>Pin</th>
<th>Function</th>
</tr>
<tr>
<td style="vertical-align: middle;text-align: center;" class="slanted-text st-red">VCC</td>
<td>Power Supply</td>
</tr>
<tr>
<td style="vertical-align: middle;text-align: center;" class="slanted-text st-yellow">Trig</td>
<td>Trigger Signal</td>
</tr>
<tr>
<td style="vertical-align: middle;text-align: center;" class="slanted-text st-teal">Echo</td>
<td>Echo Signal</td>
</tr>
<tr>
<td style="vertical-align: middle;text-align: center;" class="slanted-text st-blue">GND</td>
<td>Ground</td>
</tr>
</table>


## Measuring Distance with the HC-SR04 module

The HC-SR04 module has a transmitter and receiver, responsible for sending ultrasonic waves and detecting the reflected waves. We will use the Trig pin to send sound waves. And read from the Echo pin to measure the distance.

<img style="display: block; margin: auto;" alt="ultrasonic" src="./images/ultrasonic-trigger-echo-wave.png"/>

As you can see in the diagram, we connect the Trig and Echo pins to the GPIO pins of the microcontroller (we also connect VCC and GND but left them out to keep the illustration simple). We send ultrasonic waves by setting the Trig pin HIGH for 10 microseconds and then setting it back to LOW. This triggers the module to send 8 consecutive ultrasonic waves at a frequency of 40 kHz. It is recommended to have a minimum gap of 50ms between each trigger.

When the sensor's waves hit an object, they bounce back to the module. As you can see in the diagram, the Echo pin changes the input sent to the microcontroller, with the length of time the signal stays HIGH (pulse width) corresponding to the distance. In the microcontroller, we measure how long the Echo pin stays HIGH; Then, we can use this time duration to calculate the distance to the object.

**Pulse width and the distance:**

The pulse width (amount of time it stays high) produced by the Echo pin will range from about 150µs to 25,000µs(25ms); this is only if it hits an object. If there is no object, it will produce a pulse width of around 38ms.






File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bf902d2

Please sign in to comment.