Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98c5c20

Browse files
committed
HCSR04 for 1 pin mode sensors
1 parent e199685 commit 98c5c20

File tree

4 files changed

+126
-44
lines changed

4 files changed

+126
-44
lines changed

README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,53 @@
44
[![Build Status](https://travis-ci.org/ArminJo/Arduino-Utils.svg?branch=master)](https://travis-ci.org/ArminJo/Arduino-Utils)
55

66
## My utility collection for Arduino
7-
Contains utility files:
8-
### ADCUtils
7+
8+
### ADCUtils.cpp
99
- Fast and flexible ADC conversions.
1010
### HCSR04
1111
- Blocking and **non-blocking** reading of HCSR04 US Sensors with timeouts and exact conversions.
12-
### BlinkLed
12+
- Supports also **1 Pin mode** available with the HY-SRF05 or Parallax PING modules.
13+
#### You can modify the HCSR04 modules to 1 Pin mode by:
14+
1 Old module with 3 16 pin chips: Connect Trigger and Echo direct or use a resistor < 4.7 kOhm.
15+
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.
16+
2 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.
17+
3 New module with 1 16 pin and 2 8 pin chips: Connect Trigger and Echo by a resistor > 200 Ohm and < 22 kOhm.
18+
4 All modules: Connect Trigger and Echo by a resistor of 4.7 kOhm.
19+
20+
### BlinkLed.cpp
1321
- Class for blinking one ore more LED's in different fashions.
1422
### ShowInfo
1523
- Serial.print display of timer and other peripheral and system registers (to be extended :-)).
16-
### AVRUtils
24+
### AVRUtils.cpp
1725
- Sleep and sleep with watchdog functions.
1826
- Available Ram, Heap / Stack memory display.
19-
### MillisUtils
27+
### MillisUtils.cpp
2028
- Tweaking `millis()` after long running tasks in `noIterrupt()` context like NeoPixel output, ADC buffer reading etc.
21-
### DebugLevel
29+
### DebugLevel.h
2230
- Propagating debug levels for development. Supports level `TRACE, DEBUG, INFO, WARN and ERROR`. **Explains semantics of these levels**.
23-
### ATtinyUtils
31+
### ATtinyUtils.cpp
2432
- `changeDigisparkClock()` to use Digispark boards with standard ATtiny code like ATTinyCore by [Spence Konde](https://github.com/SpenceKonde/ATTinyCore).
2533
It changes Digispark Bootloader clock settings to get the right CPU frequency and resets Digispark OCCAL tweak.
2634
- `toneWithTimer1PWM()`.
27-
- fuse reading function.
2835
- `isBODSFlagExistent()` -> checking for ATtiny85 revision C.
29-
### The very useful *digitalWriteFast.h* file from [Watterott electronic](https://github.com/watterott/Arduino-Libs).
30-
36+
- fuse reading function.
3137

32-
### Trace.
38+
### Trace.cpp.h
3339
**Tracing an Arduino program** by printing each program counter value after executing one instruction.
3440

35-
## Utilities for serial output for ATtinies are contained in the [ATtinySerialOut](https://github.com/ArminJo/ATtinySerialOut) library available in Arduino.
41+
### [ATtinySerialOut.cpp](https://github.com/ArminJo/ATtinySerialOut/src)
42+
Minimal bit-bang send serial
43+
- 115200 baud for 1/8/16 MHz ATtiny clock.
44+
The utility for serial output for ATtinies is contained in the [ATtinySerialOut](https://github.com/ArminJo/ATtinySerialOut)
45+
library available as an Arduino library.
46+
47+
### [EasyButtonAtInt01.cpp.h](https://github.com/ArminJo/EasyButtonAtInt01/src)
48+
- Arduino library for handling push buttons just connected between ground and INT0 and / or INT1 pin.
49+
- No external pullup, **no polling needed**.
50+
The utility for easy button handling for ATmegas or ATtinies is contained in the [EasyButtonAtInt01](https://github.com/ArminJo/EasyButtonAtInt01)
51+
library available as an Arduino library.
52+
53+
### The very useful *digitalWriteFast.h* file from [Watterott electronic](https://github.com/watterott/Arduino-Libs).
3654

3755
# Modifying library properties
3856
To access the Arduino library files from a sketch, you have to first use *Sketch/Show Sketch Folder (Ctrl+K)* in the Arduino IDE.<br/>
@@ -52,8 +70,8 @@ The ArduinoUtils library examples are built on Travis CI for the following board
5270

5371
- Arduino Uno
5472
- Arduino Leonardo
55-
- Arduino Mega 2560
56-
- Arduino cplayClassic
73+
- Digispark (using ATTiny85 @1MHz)
74+
- Generic ATTiny85 @1MHz
5775

5876
## Requests for modifications / extensions
5977
Please write me a PM including your motivation/problem if you need a modification or an extension.

keywords.txt

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,48 @@
55
#######################################
66
# Datatypes (KEYWORD1)
77
#######################################
8-
Talkie KEYWORD1
8+
BlinkLed KEYWORD1
99

1010
#######################################
1111
# Methods and Functions (KEYWORD2)
1212
#######################################
13-
say KEYWORD2
14-
ptrAddr KEYWORD3
15-
sayQ KEYWORD2
16-
isTalking KEYWORD2
17-
wait KEYWORD2
18-
stop KEYWORD2
19-
terminate KEYWORD2
20-
doNotUseUseInvertedOutput KEYWORD2
21-
doNotUseNonInvertedOutput KEYWORD2
22-
getNumberOfWords KEYWORD2
23-
sayQDigit KEYWORD2
24-
sayQNumber KEYWORD2
25-
sayQFloat KEYWORD2
26-
sayQVoltageMilliVolts KEYWORD2
27-
sayQPause KEYWORD2
13+
14+
# From HCSR04.cpp
15+
initUSDistancePins KEYWORD2
16+
initUSDistancePin KEYWORD3
17+
getUSDistance KEYWORD2
18+
getCentimeterFromUSMicroSeconds KEYWORD2
19+
getUSDistanceAsCentiMeter KEYWORD2
20+
getUSDistanceAsCentiMeterWithCentimeterTimeout KEYWORD2
21+
testUSSensor KEYWORD2
22+
23+
US_DISTANCE_DEFAULT_TIMEOUT_MICROS LITERAL1
24+
US_DISTANCE_TIMEOUT_MICROS_FOR_1_METER LITERAL1
25+
US_DISTANCE_TIMEOUT_MICROS_FOR_2_METER LITERAL1
26+
US_DISTANCE_TIMEOUT_MICROS_FOR_3_METER LITERAL1
27+
US_DISTANCE_DEFAULT_TIMEOUT_CENTIMETER LITERAL1
28+
29+
# From ADCUtils.cpp
30+
readADCChannel KEYWORD2
31+
readADCChannelWithReference KEYWORD2
32+
readADCChannelWithOversample KEYWORD2
33+
readADCChannelWithReferenceOversample KEYWORD2
34+
readADCChannelWithReferenceMultiSamples KEYWORD2
35+
readADCChannelWithReferenceMax KEYWORD2
36+
readADCChannelWithReferenceMaxMicros KEYWORD2
37+
38+
getVCCVoltageSimple KEYWORD2
39+
getVCCVoltageMillivoltSimple KEYWORD2
40+
getTemperatureSimple KEYWORD2
41+
getVCCVoltage KEYWORD2
42+
getVCCVoltageMillivolt KEYWORD2
43+
printVCCVoltageMillivolt KEYWORD2
44+
getTemperature KEYWORD2
45+
46+
ADC_PRESCALE LITERAL1
47+
SHIFT_VALUE_FOR_REFERENCE LITERAL1
48+
ADC_TEMPERATURE_CHANNEL_MUX LITERAL1
49+
2850

2951
#######################################
3052
# Constants (LITERAL1)

src/HCSR04.cpp

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/*
22
* HCSR04.cpp
33
*
4-
* US Sensor (HC-SR04) functions
4+
* US Sensor (HC-SR04) functions.
55
* The non blocking functions are using pin change interrupts and need the PinChangeInterrupt library to be installed.
66
*
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+
*
714
* Copyright (C) 2018-2020 Armin Joachimsmeyer
815
916
*
@@ -27,45 +34,74 @@
2734
#include <Arduino.h>
2835
#include "HCSR04.h"
2936

30-
#define DEBUG
37+
//#define DEBUG
3138

32-
uint8_t sTriggerOutPin;
39+
uint8_t sTriggerOutPin; // also used as aTriggerOutEchoInPin for mode HCSR04_MODE_1_PIN
3340
uint8_t sEchoInPin;
34-
bool sHCSR04PinsAreInitialized = false;
3541

42+
uint8_t sHCSR04Mode = HCSR04_MODE_UNITITIALIZED;
43+
44+
/*
45+
* @param aEchoInPin - If 0 then assume 1 pin mode
46+
*/
3647
void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin) {
3748
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;
4262
}
4363

4464
/*
4565
* Start of standard blocking implementation using pulseInLong() since PulseIn gives wrong (too small) results :-(
4666
*/
4767
unsigned int getUSDistance(unsigned int aTimeoutMicros) {
48-
if (!sHCSR04PinsAreInitialized) {
68+
if (sHCSR04Mode == HCSR04_MODE_UNITITIALIZED) {
4969
return 0;
5070
}
5171

5272
// need minimum 10 usec Trigger Pulse
5373
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+
5480
#ifdef DEBUG
5581
delay(2); // to see it on scope
5682
#else
5783
delayMicroseconds(10);
5884
#endif
59-
// falling edge starts measurement
85+
// falling edge starts measurement after 400/600 microseconds (old/new modules)
6086
digitalWrite(sTriggerOutPin, LOW);
6187

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+
6297
/*
6398
* 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)
66102
* At 20 degree => 50cm gives 2914 us, 2m gives 11655 us
67103
*/
68-
unsigned long tUSPulseMicros = pulseInLong(sEchoInPin, HIGH, aTimeoutMicros);
104+
unsigned long tUSPulseMicros = pulseInLong(tEchoInPin, HIGH, aTimeoutMicros);
69105
if (tUSPulseMicros == 0) {
70106
// timeout happened
71107
tUSPulseMicros = aTimeoutMicros;

src/HCSR04.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
#define US_DISTANCE_TIMEOUT_MICROS_FOR_3_METER 17475 // Timeout of 17475 is 3 meter
3333
#define US_DISTANCE_DEFAULT_TIMEOUT_CENTIMETER 343 // Timeout of 20000L is 3.43 meter
3434

35-
void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin);
35+
void initUSDistancePins(uint8_t aTriggerOutPin, uint8_t aEchoInPin = 0);
36+
void initUSDistancePin(uint8_t aTriggerOutEchoInPin);
3637
unsigned int getUSDistance(unsigned int aTimeoutMicros = US_DISTANCE_DEFAULT_TIMEOUT_MICROS);
3738
unsigned int getCentimeterFromUSMicroSeconds(unsigned int aDistanceMicros);
3839
unsigned int getUSDistanceAsCentiMeter(unsigned int aTimeoutMicros = US_DISTANCE_DEFAULT_TIMEOUT_MICROS);
@@ -49,6 +50,11 @@ extern unsigned int sUSDistanceCentimeter;
4950
extern volatile unsigned long sUSPulseMicros;
5051
#endif
5152

53+
#define HCSR04_MODE_UNITITIALIZED 0
54+
#define HCSR04_MODE_USE_1_PIN 1
55+
#define HCSR04_MODE_USE_2_PINS 2
56+
extern uint8_t sHCSR04Mode;
57+
5258
#endif // HCSR04_H_
5359

5460
#pragma once

0 commit comments

Comments
 (0)