MPL3115A2 pressure sensor program
Model number : MPL3115A2 module
Connection
Vin → 3.3V (supply voltage)
GND → GND
3vo → 3.3V
INT1 → Non connection
INT2 → Non connection
SCL → p27 (mbed SCL)
SDA → p28 (mbed SDA)
main.cpp@0:af2cf0c5c15c, 2019-11-06 (annotated)
- Committer:
- HeptaSatTraining2019
- Date:
- Wed Nov 06 18:51:53 2019 +0000
- Revision:
- 0:af2cf0c5c15c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HeptaSatTraining2019 | 0:af2cf0c5c15c | 1 | #include "mbed.h" |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 2 | #include "MPL3115A2.h" |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 3 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 4 | I2C i2c(p28, p27); // sda, scl |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 5 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 6 | // Comment out all of the references to 'pc' on this page if you don't have the |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 7 | // serial debug driver for your mbed board installed on your computer. If you do, |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 8 | // I personally like to use Putty as the terminal window to capture debug messages. |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 9 | Serial pc(USBTX, USBRX); // tx, rx |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 10 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 11 | // Again, remove the '&pc' parameter is you're not debugging. |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 12 | MPL3115A2 sensor(&i2c, &pc); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 13 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 14 | DigitalOut myled(LED1); // Sanity check to make sure the program is working. |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 15 | DigitalOut powerPin(p21); // <-- I powered the sensor from a pin. You don't have to. |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 16 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 17 | int main() { |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 18 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 19 | powerPin = 1; |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 20 | wait_ms(300); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 21 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 22 | pc.printf("** MPL3115A2 SENSOR **\r\n"); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 23 | sensor.init(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 24 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 25 | Altitude a; |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 26 | Temperature t; |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 27 | Pressure p; |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 28 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 29 | // Offsets for Dacula, GA |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 30 | sensor.setOffsetAltitude(83); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 31 | sensor.setOffsetTemperature(20); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 32 | sensor.setOffsetPressure(-32); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 33 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 34 | while(1) |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 35 | { |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 36 | sensor.readAltitude(&a); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 37 | sensor.readTemperature(&t); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 38 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 39 | sensor.setModeStandby(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 40 | sensor.setModeBarometer(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 41 | sensor.setModeActive(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 42 | sensor.readPressure(&p); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 43 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 44 | pc.printf("Altitude: %sft, Temp: %sºF, Pressure: %sPa\r\n", a.print(), t.print(), p.print()); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 45 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 46 | sensor.setModeStandby(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 47 | sensor.setModeAltimeter(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 48 | sensor.setModeActive(); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 49 | |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 50 | wait(1.0); |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 51 | } |
HeptaSatTraining2019 | 0:af2cf0c5c15c | 52 | } |