Skip to content

Commit 9598969

Browse files
fredeilkrwq
authored andcommitted
HC-SR04 device binding (#153)
* Initial commit * Remove tests add GetDistance * Add sample * Update sample * Ref perform both operations at once * Fix some review * Update code to resemble krwq * Clean up the readmes * Add fritzing diagram * Add 1kohm resistor * Fix class docs
1 parent eb7665a commit 9598969

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

src/devices/HCSR04/HCSR04.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Diagnostics;
7+
using System.Device.Gpio;
8+
using System.Device.I2c;
9+
using System.Device.I2c.Drivers;
10+
using System.Device.Spi;
11+
using System.Device.Spi.Drivers;
12+
using System.Threading;
13+
14+
namespace Iot.Device.HCSR04
15+
{
16+
public class Sonar : IDisposable
17+
{
18+
private readonly int _echo;
19+
private readonly int _trigger;
20+
private GpioController _controller;
21+
private Stopwatch _timer = new Stopwatch();
22+
23+
/// <summary>
24+
/// Creates a new instance of the HC-SCR04 sonar.
25+
/// </summary>
26+
/// <param name="triggerPin">Trigger pulse input.</param>
27+
/// <param name="echoPin">Trigger pulse output.</param>
28+
public Sonar(int triggerPin, int echoPin)
29+
{
30+
_echo = echoPin;
31+
_trigger = triggerPin;
32+
_controller = new GpioController();
33+
34+
_controller.OpenPin(_echo, PinMode.Input);
35+
_controller.OpenPin(_trigger, PinMode.Output);
36+
37+
_controller.Write(_trigger, PinValue.Low);
38+
}
39+
40+
/// <summary>
41+
/// Gets the current distance in cm.
42+
/// </summary>
43+
public double GetDistance()
44+
{
45+
_timer.Reset();
46+
47+
// Trigger input for 10uS to start ranging
48+
// ref https://components101.com/sites/default/files/component_datasheet/HCSR04%20Datasheet.pdf
49+
_controller.Write(_trigger, PinValue.High);
50+
Thread.Sleep(TimeSpan.FromMilliseconds(0.01));
51+
_controller.Write(_trigger, PinValue.Low);
52+
53+
_timer.Start();
54+
55+
while(_controller.Read(_echo) == PinValue.Low)
56+
{
57+
}
58+
59+
while(_controller.Read(_echo) == PinValue.High)
60+
{
61+
}
62+
63+
_timer.Stop();
64+
65+
TimeSpan elapsed = _timer.Elapsed;
66+
67+
// distance = (time / 2) × velocity of sound (34300 cm/s)
68+
return (double)(elapsed.TotalMilliseconds * 34.3) / 2 - 8;
69+
}
70+
71+
public void Dispose()
72+
{
73+
if(_controller != null)
74+
{
75+
_controller.Dispose();
76+
_controller = null;
77+
}
78+
}
79+
}
80+
}

src/devices/HCSR04/HCSR04.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<EnableDefaultItems>false</EnableDefaultItems>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" />
10+
<Compile Include="HCSR04.cs" />
11+
<None Include="README.md" />
12+
</ItemGroup>
13+
14+
</Project>

src/devices/HCSR04/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# HC-SR04
2+
3+
Device bindings for the HC-SR04 sonar distance sensor.
4+
5+
## Summary
6+
7+
Calculates the distance from an object by using the HC-SR04 distance sensor.
8+
9+
## Binding Notes
10+
11+
Provide any specifics related to binding API. This could include how to configure component for particular functions and example code.
12+
13+
## References
14+
15+
* [HC-SR04 data sheet](https://components101.com/sites/default/files/component_datasheet/HCSR04%20Datasheet.pdf)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Device.Gpio;
7+
using System.Device.I2c;
8+
using System.Device.I2c.Drivers;
9+
using System.Device.Spi;
10+
using System.Device.Spi.Drivers;
11+
using System.Threading;
12+
using Iot.Device.HCSR04;
13+
14+
namespace Iot.Device.HCSR04.Samples
15+
{
16+
class Program
17+
{
18+
static void Main(string[] args)
19+
{
20+
Console.WriteLine("Hello HCSR04 Sample!");
21+
22+
using(var sonar = new Iot.Device.HCSR04.Sonar(4, 17))
23+
{
24+
while(true)
25+
{
26+
Console.WriteLine($"Value = {sonar.GetDistance()}");
27+
System.Threading.Thread.Sleep(100);
28+
}
29+
}
30+
}
31+
}
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="../HCSR04.csproj" />
10+
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease*" />
11+
</ItemGroup>
12+
13+
</Project>

src/devices/HCSR04/samples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Distance sensor sample (using HC-SR04)
2+
3+
This simple example shows how to use the HC-SR04 device binding with dotnet/iot.
4+
5+
## Breadboard layout
6+
7+
The following fritz diagram depicts how you should wire your RPi in order to run the program, the resistance for R1 is 1kOhm.
8+
9+
![Fritz diagram](raspberry_hc-sr04.png)
319 KB
Loading

0 commit comments

Comments
 (0)