Skip to content

Instantly share code, notes, and snippets.

@bkralik
Last active January 12, 2022 07:45
Show Gist options
  • Save bkralik/9b444d58df27ee14fcdf6cf2b4047413 to your computer and use it in GitHub Desktop.
Save bkralik/9b444d58df27ee14fcdf6cf2b4047413 to your computer and use it in GitHub Desktop.
Arduino sketch to interface Hella OPS+T sensor
// HELLA OPS+T Combined digital sender (Oil pressure and temperature in one package)
// Usage: look at setup() and loop().
// Settings
const byte hella_pin = 2; // Pin to connect Hella OPS+T to (must be 2 or 3 on regular Arduino)
const byte hella_count = 16; // Number of time samples to capture - higher number for higher precision but lower refreshrate
// Variables
volatile uint32_t hella_measured[hella_count];
volatile uint8_t hella_num = 0;
volatile uint8_t hella_finished = 0;
// Containers for result
uint8_t hella_state = 0;
float hella_temp = 0.0;
float hella_pres = 0.0;
void setup() {
pinMode(hella_pin, INPUT);
Serial.begin(115200);
}
void loop() {
hellaStart();
hellaProcess();
hellaPrintOut();
delay(1000);
}
void hellaStart() {
hella_num = 0;
hella_finished = 0;
attachInterrupt(digitalPinToInterrupt(hella_pin), hellaInterrupt, FALLING);
while(hella_finished == 0) {}
}
void hellaInterrupt() {
hella_measured[hella_num] = micros();
// little magic to ensure that first captured edge will always be falling
if(hella_num == 1) {
attachInterrupt(digitalPinToInterrupt(hella_pin), hellaInterrupt, CHANGE);
}
hella_num++;
if(hella_num == hella_count) {
detachInterrupt(digitalPinToInterrupt(hella_pin));
hella_finished = 1;
}
}
void hellaProcess() {
// Compute widths of pulses
uint32_t widths[8];
for(uint8_t i = 2; i < hella_count; i = i + 2) {
widths[i / 2] = hella_measured[i + 1] - hella_measured[i];
}
// Compute periods of pulses
uint32_t periods[8];
for(uint8_t i = 1; i < (hella_count/2 - 1); i++) {
periods[i] = hella_measured[2*i+2] - hella_measured[2*i];
}
// Find the first pulse
uint8_t starter = 0;
for(uint8_t i = 1; i < (hella_count/2 - 1); i++) {
if((periods[i] > 950) && (periods[i] < 1150)) {
starter = i;
break;
}
}
// Compute state
uint32_t state = widths[starter] * 1024 / periods[starter];
// Compute temperature pulse length
uint32_t temp_digital = widths[starter+1] * 4096 / periods[starter+1];
// Compute pressure pulse length
uint32_t pres_digital = widths[starter+2] * 4096 / periods[starter+2];
//Serial.println(pres_digital);
// Recompute to final float vaules
float temp = (temp_digital - 128) / 3840.0 * 200.0 - 40.0;
float pres = (pres_digital - 128) / 3840.0 * 10.0 + 0.5;
}
void hellaPrintOut() {
// Print out the results
Serial.print(temp);
Serial.write(" degC\n");
Serial.print(pres);
Serial.write(" bar\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment