Created
February 7, 2025 01:40
-
-
Save ancientstraits/3dc3fc0bf09eea8f44b336f710cbf1c0 to your computer and use it in GitHub Desktop.
Galactech Teensy 4.0 Light Sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(0, OUTPUT); | |
Wire.begin(); | |
Serial.begin(115200); | |
Wire.beginTransmission(0x52); | |
// MAIN_CTRL (only turn on proximity sensor) | |
Wire.send(0x00); | |
Wire.send(0b00000111); | |
// PS_MEAS_RATE (11-bit precision, measures every 12.5 milliseconds); | |
Wire.send(0x03); | |
Wire.send(0b00011010); | |
Wire.endTransmission(); | |
} | |
byte first, second; | |
bool overflow; | |
int proximity; | |
int error; | |
void loop() { | |
Wire.beginTransmission(0x52); | |
// Ask for PS_DATA | |
Wire.write(0x08); | |
int error = Wire.endTransmission(); | |
if (error != 0) { | |
Serial.print("ERROR!!! "); | |
Serial.println(error); | |
} else { | |
Serial.println("OK"); | |
} | |
Wire.requestFrom(0x52, 2); | |
first = Wire.read(); | |
second = Wire.read(); | |
overflow = (second >> 3) == 1; | |
proximity = (int)(second & 0b111) << 8; | |
proximity = proximity | first; | |
Serial.println(proximity); | |
digitalWrite(0, (proximity > 15) ? HIGH : LOW); | |
delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment