Skip to content

Commit

Permalink
Improved US-100 support
Browse files Browse the repository at this point in the history
  • Loading branch information
pawel-spychalski committed Aug 3, 2017
1 parent 5de71aa commit d26ef6e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"board": "ATTinyCore:avr:attinyx5",
"configuration": "TimerClockSource=default,chip=85,clock=8internal,bod=disable",
"configuration": "chip=85,clock=8internal,bod=disable",
"sketch": "inav_i2c_sonar.ino"
}
12 changes: 12 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
]
},
"intelliSenseMode": "msvc-x64"
},
{
"name": "Mac",
"includePath": [
"/Users/pspychalski/Library/Arduino15/packages/ATTinyCore/hardware/avr/1.0.6/cores/empty",
"/Users/pspychalski/Library/Arduino15/packages/ATTinyCore/hardware/avr/1.0.6/cores/tiny",
"/Users/pspychalski/Library/Arduino15/packages/ATTinyCore/hardware/avr/1.0.6/cores/tinyNoMillis"
],
"browse": {
"limitSymbolsToIncludedHeaders": false
},
"intelliSenseMode": "clang-x64"
}
],
"version": 2
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# I2C interface for HC-SR04 sonar rangefinder
# I2C interface for HC-SR04 and US-100 sonar rangefinder

Most modern flight controllers does not allows to connect HC-SR04 rangefinders. Reason in quite simple: not enough pins for quite low priority device.
Most modern flight controllers does not allows to connect HC-SR04 and US-100 rangefinders. Reason in quite simple: not enough pins for quite low priority device.

This interface can be used to connect HC-SR04 sonar rangefinder to boards equipped with I2C bus and running latest versions of INAV.
This interface can be used to connect HC-SR04 and US-100 sonar rangefinder to boards equipped with I2C bus and running latest versions of INAV.

## US-100 instead of HC-SR04

**US-100** is superior to HC-SR04 in almost all areas. While HC-SR04 interface is doable, it is _HIGHLY_ recommended to use US-100 instead!

On a hardware level, connection is the same: Echo and Trigger pin. Condition is to switch US-100 to _trigger/echo_ by removing a jumper in be back of the board. When jumper is removed, US-100 will behive almost exactly like HC-SR04.

## Diagram

Expand All @@ -11,8 +17,10 @@ This interface can be used to connect HC-SR04 sonar rangefinder to boards equipp
## Notes

* HC-SR04 is 5V, while STM32 CPU are 3.3V devices and might not tolerate 5V on I2C lines. This is why, you either have to run ATtiny on 3.3V or run it on 5V too (remove inline resistors) but connect using logic level shifters
* US-100 can work in both 3.3V and 5V mode. 3.3V mode was not tested, that is why inline resistors are in place
* You can remove 3.3V stabilizer and take 3.3V from flight controller too
* Latest Arduino IDE (suggested 1.8.x, should work with older)
* [ATtiny Universal](https://github.com/SpenceKonde/ATTinyCore) ATtiny core files
* [TinyWireS](https://github.com/rambo/TinyWire/tree/master/TinyWireS) library
* IPS programmer. I use USBasp, but even Arduino as ISP can be used
* [ATtiny flashing guide](https://quadmeup.com/programming-attiny85-and-attiny45-with-arduino-ide/)
34 changes: 27 additions & 7 deletions inav_i2c_sonar.ino
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* Decide on rangefinder type here. Only one type should be uncommented
*/
#define USE_US100
// #define USE_HCSR04

/*
* Set I2C Slave address
*/
#define I2C_SLAVE_ADDRESS 0x14

#define PULSE_TO_CM 68
#define MAX_RANGE 400 //Range of 4 meters
#define PULSE_TIMEOUT (MAX_RANGE * PULSE_TO_CM) //this is an equivalent of 10 meters range

#define DEBUG;
// #define DEBUG;

#define TRIGGER_PIN 3
#define ECHO_PIN 4
Expand All @@ -24,6 +26,24 @@
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif

/*
* Configuration magic, do not touch
*/
#ifdef USE_US100
#define PULSE_TO_CM 59 //Multiplier pulse length to distance in [cm]
#define MEASUREMENT_PERIOD_RATIO 6 // measurement rate = MEASUREMENT_PERIOD_RATIO * 16, 96ms in this case
#define MAX_RANGE 300 //Range of 4 meters
#endif

#ifdef USE_HCSR04
#define PULSE_TO_CM 58 //Multiplier pulse length to distance in [cm]
#define MEASUREMENT_PERIOD_RATIO 5 // measurement rate = MEASUREMENT_PERIOD_RATIO * 16, 80ms in this case
#define MAX_RANGE 300 //Range of 4 meters
#endif

#define PULSE_TIMEOUT (MAX_RANGE * PULSE_TO_CM) //this is an equivalent of 4 meters range


volatile uint8_t i2c_regs[] =
{
0, //status
Expand Down Expand Up @@ -153,7 +173,7 @@ void loop() {
/*
* Measurement is done every 6th wakeup, that gives more less 10Hz update rate (96ms)
*/
if (wakeCounter == 6) {
if (wakeCounter == MEASUREMENT_PERIOD_RATIO) {

digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
Expand Down Expand Up @@ -185,4 +205,4 @@ void loop() {

wakeCounter = 0;
}
}
}

0 comments on commit d26ef6e

Please sign in to comment.