Digital Output Barometer LPS25H

This exercise explains how to connect opportunely our Arduino Yún board to the pressure sensor LPS25H and how to program it to get the values for the atmospheric pressure, temperature and altitude. 

The circuit

Image  

Be sure to follow the connections shown in the fritzing circuit and in the next table:

     LPS25H sensor           Arduino Yún board

  •  pin1             --->    5V
  •  pin2             --->    D3 
  •  pin3             --->    D2
  •  pin8             --->    GND

The code

In the next sketch we will use the LPS library. Download and install the "LPS" folder into the "libraries" subdirectory inside your Arduino sketchbook directory (see referral links ). 
Write or paste code in Arduino IDE, then load the sketch on Yun board. 
This program takes pressure and temperature readings every two seconds (settable with delay) from the sensor and calculates an altitude from the pressure. It shows the data in metric units: pressure in millibars (mbar), altitude in meters, and temperature in degrees Celsius.
#include <Wire.h>
#include <LPS.h> LPS ps; void setup() { Serial1.begin(230400); Wire.begin(); if (!ps.init()) { Serial1.println("Failed to autodetect pressure sensor!"); while (1); } ps.enableDefault(); } void loop() { float pressure = ps.readPressureMillibars(); float altitude = ps.pressureToAltitudeMeters(pressure); float temperature = ps.readTemperatureC(); Serial1.print(" Pressure: "); Serial1.print(pressure); Serial1.print(" mbar\ Altitude: "); Serial1.print(altitude); Serial1.print(" m\ Temperature: "); Serial1.print(temperature); Serial1.println(" deg C"); delay(2000); } #include #include LPS ps; void setup() { Serial1.begin(230400); Wire.begin(); if (!ps.init()) { Serial1.println("Failed to autodetect pressure sensor!"); while (1); } ps.enableDefault(); } void loop() { float pressure = ps.readPressureMillibars(); float altitude = ps.pressureToAltitudeMeters(pressure); float temperature = ps.readTemperatureC(); Serial1.print(" Pressure: "); Serial1.print(pressure); Serial1.print(" mbar\ Altitude: "); Serial1.print(altitude); Serial1.print(" m\ Temperature: "); Serial1.print(temperature); Serial1.println(" deg C"); delay(2000); } Python Serial Test Read data from shell. To do this we used the included library “pyserial”. You must create a python script to be launched from a terminal. Then connect to your board with ssh, create file serialTest.py with the following code and start it with the command “python serialTest.py”. Read data now. import serial try: print "Trying..." arduino = serial.Serial('/dev/ttyATH0',230400) except: print "Failed to connect on" while True: x=arduino.readline() print x import serial try: print "Trying..." arduino = serial.Serial('/dev/ttyATH0',230400) except: print "Failed to connect on" while True: x=arduino.readline() print x

Python Serial Test

Read data from shell.  
To do this we used the included library “pyserial”. You must create a python script to be launched from a terminal. Then connect to your board with ssh, create file serialTest.py with the following code and start it with the command  “python serialTest.py”. Read data now.
import serial
try:
     print "Trying..."
     arduino = serial.Serial('/dev/ttyATH0',230400)
except:
     print "Failed to connect on"
while True:
     x=arduino.readline()
     print x

Referral links:

Digital output barometer LPS25H
Arduino library for LPS25H