Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xcvista
Created May 22, 2014 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcvista/d54877453d149e30f112 to your computer and use it in GitHub Desktop.
Save xcvista/d54877453d149e30f112 to your computer and use it in GitHub Desktop.
A GPIO echo meter that sends echo time length over I2C
#include <Wire.h>
uint32_t ms;
int mode;
#define WIRE_ADDR 0x8
#define TRIGGER 38
#define ECHO 36
void setup()
{
// Set up sensor
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(13, OUTPUT);
mode = 1;
// Start I2C
Wire.begin(WIRE_ADDR);
Wire.onRequest(i2cRead);
}
void loop()
{
// Make measuremrnt
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
ms = pulseIn(ECHO, HIGH, 1UL << 22);
digitalWrite(13, mode);
mode = !mode;
delay(10);
}
void i2cRead()
{
Wire.write((const char *)&ms, sizeof(ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment