Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.