|
1 | 1 | /* |
2 | 2 | * HCSR04.cpp |
3 | 3 | * |
4 | | - * US Sensor (HC-SR04) functions |
| 4 | + * US Sensor (HC-SR04) functions. |
5 | 5 | * The non blocking functions are using pin change interrupts and need the PinChangeInterrupt library to be installed. |
6 | 6 | * |
| 7 | + * Supports also 1 Pin mode. You can modify the HCSR04 modules to 1 Pin mode by: |
| 8 | + * Old module with 3 16 pin chips: Connect Trigger and Echo direct or use a resistor < 4.7 kOhm. |
| 9 | + * If you remove both 10 kOhm pullup resistor you can use a connecting resistor < 47 kOhm, but I suggest to use 10 kOhm which is more reliable. |
| 10 | + * Old module with 3 16 pin chips but with no pullup resistors near the connector row: Connect Trigger and Echo with a resistor > 200 Ohm. Use 10 kOhm. |
| 11 | + * New module with 1 16 pin and 2 8 pin chips: Connect Trigger and Echo by a resistor > 200 Ohm and < 22 kOhm. |
| 12 | + * All modules: Connect Trigger and Echo by a resistor of 4.7 kOhm. |
| 13 | + * |
7 | 14 | * Copyright (C) 2018-2020 Armin Joachimsmeyer |
8 | 15 | |
9 | 16 | * |
|
27 | 34 | #include <Arduino.h> |
28 | 35 | #include "HCSR04.h" |
29 | 36 |
|
30 | | -#define DEBUG |
| 37 | +//#define DEBUG |
31 | 38 |
|
32 | | -uint8_t sTriggerOutPin; |
| 39 | +uint8_t sTriggerOutPin; // also used as aTriggerOutEchoInPin for mode HCSR04_MODE_1_PIN |
33 | 40 | uint8_t sEchoInPin; |
34 | | -bool sHCSR04PinsAreInitialized = false; |
35 | 41 |
|
| 42 | +uint8_t sHCSR04Mode = HCSR04_MODE_UNITITIALIZED; |
| 43 | + |
| 44 | +/* |
| 45 | + * @param aEchoInPin - If 0 then assume 1 pin mode |
| 46 | + */ |
36 | 47 | void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin) { |
37 | 48 | sTriggerOutPin = aTriggerOutPin; |
38 | | - sEchoInPin = aEchoInPin; |
39 | | - pinMode(aTriggerOutPin, OUTPUT); |
40 | | - pinMode(sEchoInPin, INPUT); |
41 | | - sHCSR04PinsAreInitialized = true; |
| 49 | + if (aEchoInPin == 0) { |
| 50 | + sHCSR04Mode = HCSR04_MODE_USE_1_PIN; |
| 51 | + } else { |
| 52 | + sEchoInPin = aEchoInPin; |
| 53 | + pinMode(aTriggerOutPin, OUTPUT); |
| 54 | + pinMode(sEchoInPin, INPUT); |
| 55 | + sHCSR04Mode = HCSR04_MODE_USE_2_PINS; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void initUSDistancePin(uint8_t aTriggerOutEchoInPin) { |
| 60 | + sTriggerOutPin = aTriggerOutEchoInPin; |
| 61 | + sHCSR04Mode = HCSR04_MODE_USE_1_PIN; |
42 | 62 | } |
43 | 63 |
|
44 | 64 | /* |
45 | 65 | * Start of standard blocking implementation using pulseInLong() since PulseIn gives wrong (too small) results :-( |
46 | 66 | */ |
47 | 67 | unsigned int getUSDistance(unsigned int aTimeoutMicros) { |
48 | | - if (!sHCSR04PinsAreInitialized) { |
| 68 | + if (sHCSR04Mode == HCSR04_MODE_UNITITIALIZED) { |
49 | 69 | return 0; |
50 | 70 | } |
51 | 71 |
|
52 | 72 | // need minimum 10 usec Trigger Pulse |
53 | 73 | digitalWrite(sTriggerOutPin, HIGH); |
| 74 | + |
| 75 | + if (sHCSR04Mode == HCSR04_MODE_USE_1_PIN) { |
| 76 | + // do it AFTER digitalWrite to avoid spurious triggering by just switching pin to output |
| 77 | + pinMode(sTriggerOutPin, OUTPUT); |
| 78 | + } |
| 79 | + |
54 | 80 | #ifdef DEBUG |
55 | 81 | delay(2); // to see it on scope |
56 | 82 | #else |
57 | 83 | delayMicroseconds(10); |
58 | 84 | #endif |
59 | | -// falling edge starts measurement |
| 85 | +// falling edge starts measurement after 400/600 microseconds (old/new modules) |
60 | 86 | digitalWrite(sTriggerOutPin, LOW); |
61 | 87 |
|
| 88 | + uint8_t tEchoInPin; |
| 89 | + if (sHCSR04Mode == HCSR04_MODE_USE_1_PIN) { |
| 90 | + delayMicroseconds(10); // allow for 10 us low before switching to input which is high because of the modules pullup resistor. |
| 91 | + pinMode(sTriggerOutPin, INPUT); |
| 92 | + tEchoInPin = sTriggerOutPin; |
| 93 | + } else { |
| 94 | + tEchoInPin = sEchoInPin; |
| 95 | + } |
| 96 | + |
62 | 97 | /* |
63 | 98 | * Get echo length. |
64 | | - * Speed of sound at 20 degree is 343,46 m/s => 58,23 us per centimeter and 17,17 cm/ms (forth and back) |
65 | | - * Speed of sound at 10 degree is 337,54 m/s => 59,25 us per centimeter and 16,877 cm/ms (forth and back) |
| 99 | + * Speed of sound is: 331.5 + (0.6 * TemperatureCelsius). |
| 100 | + * Exact value at 20 degree is 343,46 m/s => 58,23 us per centimeter and 17,17 cm/ms (forth and back) |
| 101 | + * Exact value at 10 degree is 337,54 m/s => 59,25 us per centimeter and 16,877 cm/ms (forth and back) |
66 | 102 | * At 20 degree => 50cm gives 2914 us, 2m gives 11655 us |
67 | 103 | */ |
68 | | - unsigned long tUSPulseMicros = pulseInLong(sEchoInPin, HIGH, aTimeoutMicros); |
| 104 | + unsigned long tUSPulseMicros = pulseInLong(tEchoInPin, HIGH, aTimeoutMicros); |
69 | 105 | if (tUSPulseMicros == 0) { |
70 | 106 | // timeout happened |
71 | 107 | tUSPulseMicros = aTimeoutMicros; |
|
0 commit comments