Skip to content

Instantly share code, notes, and snippets.

@ancientstraits
Created February 22, 2025 19:20
Show Gist options
  • Save ancientstraits/5ded0b4e952ae49fc38aaaa38feae2f1 to your computer and use it in GitHub Desktop.
Save ancientstraits/5ded0b4e952ae49fc38aaaa38feae2f1 to your computer and use it in GitHub Desktop.
THIS 100% WORKS!!!
#include <Wire.h>
void sensorSetup(TwoWire& sensor) {
sensor.begin();
sensor.beginTransmission(0x52);
// MAIN_CTRL (only turn on proximity sensor)
sensor.send(0x00);
sensor.send(0b00000111);
// PS_MEAS_RATE (11-bit precision, measures every 12.5 milliseconds);
sensor.send(0x03);
sensor.send(0b00011010);
sensor.endTransmission();
}
void sensorProximity(TwoWire& sensor, int thing) {
byte first, second;
bool overflow;
int proximity;
int error;
sensor.beginTransmission(0x52);
// Ask for PS_DATA
sensor.write(0x08);
error = sensor.endTransmission();
if (error != 0) {
Serial.print("ERROR!!! ");
Serial.println(thing);
} else {
Serial.println("OK...");
}
sensor.requestFrom(0x52, 2);
first = sensor.read();
second = sensor.read();
overflow = (second >> 3) == 1;
proximity = (int)(second & 0b111) << 8;
proximity = proximity | first;
Serial.print(thing);
Serial.print(" ");
Serial.println(proximity);
// digitalWrite(0, (proximity > 15) ? HIGH : LOW);
}
void setup() {
Serial.begin(115200);
sensorSetup(Wire);
sensorSetup(Wire1);
}
void loop() {
sensorProximity(Wire, 0);
sensorProximity(Wire1, 1);
delay(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment