Created
October 3, 2019 10:47
-
-
Save pral2a/2220b10e6411f81e2dc350f86823dc7f to your computer and use it in GitHub Desktop.
I2CADCforPi
This file contains 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> | |
/* | |
* Set I2C Slave address | |
*/ | |
#define I2C_SLAVE_ADDRESS 0x7 | |
#define SENSOR A0 | |
// Measurement | |
#define MAX_TICK 50 | |
unsigned int tick = 0; | |
//Smoothing Factor | |
#define LPF_FACTOR 0.5 | |
unsigned long lastReadout = 0; | |
// Variables for the pressure sensor | |
int _delay = 100; | |
float Va = 5; | |
int readSensor = 0; | |
float Vs = 0; | |
float pressure = 0; | |
float pressure_bar = 0; | |
// I2C Stuff | |
volatile uint8_t i2c_regs[] = | |
{ | |
0, //older 8 | |
0, | |
0, | |
0 //younger 8 | |
}; | |
volatile byte reg_position = 0; | |
const byte reg_size = sizeof(i2c_regs); | |
volatile unsigned long pressureSmooth; | |
void requestEvent() | |
{ | |
Wire.write(i2c_regs[reg_position]); | |
reg_position++; | |
if (reg_position >= reg_size) | |
{ | |
reg_position = 0; | |
} | |
} | |
void setup() { | |
/* | |
* Setup I2C | |
*/ | |
Wire.begin(I2C_SLAVE_ADDRESS); | |
Wire.onRequest(requestEvent); | |
} | |
int smooth(int data, float filterVal, long smoothedVal){ | |
if (filterVal > 1){ // check to make sure params are within range | |
filterVal = .99; | |
} | |
else if (filterVal <= 0){ | |
filterVal = 0; | |
} | |
smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal); | |
return (int)smoothedVal; | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
/* | |
* On tick value 0, do measurements | |
*/ | |
if (abs(currentMillis - lastReadout) > MAX_TICK) { | |
// Read the values | |
int sensorReading = analogRead(SENSOR); | |
// Treat them | |
Vs = ((float) sensorReading + 0.5 ) / 1024.0 * 5.0; | |
pressure = (Vs*687.8/Va - 18.77)*100; // in DPa | |
// Smooth it | |
pressureSmooth = smooth(pressure, LPF_FACTOR, pressureSmooth); // in Pa | |
// Put the smoothed vaue into the I2C register | |
i2c_regs[0] = pressureSmooth >> 24 & 0xFF; | |
i2c_regs[1] = pressureSmooth >> 16 & 0xFF; | |
i2c_regs[2] = pressureSmooth >> 8 & 0xFF; | |
i2c_regs[3] = pressureSmooth & 0xFF; | |
// Update the last readout | |
lastReadout = currentMillis; | |
} | |
} |
This file contains 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
import smbus | |
import time | |
bus = smbus.SMBus(1) # Indicates /dev/i2c-1 | |
address = 0x7 | |
packet_size = 4 | |
def ReadSensor(_address): | |
i = 0 | |
_value = 0 | |
while (i < packet_size): | |
_measure = bus.read_i2c_block_data(_address, 0, 1) | |
#~ print "Measure" | |
#~ print _measure | |
_value |= _measure[0] << (8*(packet_size-(1+i))) | |
i+=1 | |
return _value | |
while True: | |
result = ReadSensor(address) | |
#~ print "Result" | |
print result | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment