Skip to content

Commit e1ba91a

Browse files
committed
Added Barometric pressure sensor example for SPI library
1 parent 98cb2e4 commit e1ba91a

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
SCP1000 Barometric Pressure Sensor Display
3+
4+
Shows the output of a Barometric Pressure Sensor on a
5+
Uses the SPI library. For details on the sensor, see:
6+
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
7+
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8+
9+
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10+
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11+
12+
Circuit:
13+
SCP1000 sensor attached to pins 6, 7, 10 - 13:
14+
DRDY: pin 6
15+
CSB: pin 7
16+
MOSI: pin 11
17+
MISO: pin 12
18+
SCK: pin 13
19+
20+
created 31 July 2010
21+
by Tom Igoe
22+
*/
23+
24+
// the sensor communicates using SPI, so include the library:
25+
#include <SPI.h>
26+
27+
//Sensor's memory register addresses:
28+
const int PRESSURE = 0x1F; //3 most significant bits of pressure
29+
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
30+
const int TEMPERATURE = 0x21; //16 bit temperature reading
31+
32+
// pins used for the connection with the sensor
33+
// the other you need are controlled by the SPI library):
34+
const int dataReadyPin = 6;
35+
const int chipSelectPin = 7;
36+
37+
void setup() {
38+
Serial.begin(9600);
39+
// initalize the data ready and chip select pins:
40+
pinMode(dataReadyPin, INPUT);
41+
pinMode(chipSelectPin, OUTPUT);
42+
43+
// start the SPI library:
44+
SPI.begin();
45+
46+
//Configure SCP1000 for low noise configuration:
47+
writeRegister(0x02, 0x2D);
48+
writeRegister(0x01, 0x03);
49+
writeRegister(0x03, 0x02);
50+
// give the sensor time to set up:
51+
delay(100);
52+
}
53+
54+
void loop() {
55+
//Select High Resolution Mode
56+
writeRegister(0x03, 0x0A);
57+
58+
// don't do anything until the data ready pin is high:
59+
if (digitalRead(dataReadyPin) == HIGH) {
60+
//Read the temperature data
61+
int tempData = readRegister(0x21, 2);
62+
63+
// convert the temperature to celsius and display it:
64+
float realTemp = (float)tempData / 20.0;
65+
Serial.print("Temp[C]=");
66+
Serial.print(realTemp);
67+
68+
69+
//Read the pressure data highest 3 bits:
70+
byte pressure_data_high = readRegister(0x1F, 1);
71+
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
72+
73+
//Read the pressure data lower 16 bits:
74+
unsigned int pressure_data_low = readRegister(0x20, 2);
75+
//combine the two parts into one 19-bit number:
76+
long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
77+
78+
// display the temperature:
79+
Serial.println("\tPressure [Pa]=" + String(pressure));
80+
}
81+
}
82+
83+
//Sends a write command to SCP1000
84+
85+
void writeRegister(byte registerName, byte registerValue) {
86+
// SCP1000 expects the reguster name in the upper 6 bits
87+
// of the byte:
88+
registerName <<= 2;
89+
// command goes in the lower two bits:
90+
registerName |= 0b00000010; //Write command
91+
92+
// take the chip select low to select the device:
93+
digitalWrite(chipSelectPin, LOW);
94+
95+
SPI.send(registerName); //Send register location
96+
SPI.send(registerValue); //Send value to record into register
97+
98+
// take the chip select high to de-select:
99+
digitalWrite(chipSelectPin, HIGH); //Select SPI device
100+
}
101+
102+
103+
//Read register from the SCP1000:
104+
unsigned int readRegister(byte registerName, int numBytes) {
105+
byte inByte = 0; // incoming from the SPI read
106+
unsigned int result = 0; // result to return
107+
108+
// SCP1000 expects the reguster name in the upper 6 bits
109+
// of the byte:
110+
registerName <<= 2;
111+
// command goes in the lower two bits:
112+
registerName &= 0b11111100; //Read command
113+
114+
// take the chip select low to select the device:
115+
digitalWrite(chipSelectPin, LOW);
116+
// send the device the register you want to read:
117+
int command = SPI.send(registerName);
118+
// send a value of 0 to read the forst byte returned:
119+
inByte = SPI.send(0x00);
120+
121+
// if there's more than one byte returned,
122+
// shift the first byte then get the second byte:
123+
switch(numBytes) {
124+
case 1:
125+
result = inByte;
126+
break;
127+
case 2:
128+
result = inByte << 8;
129+
inByte = SPI.send(0x00);
130+
result = result |inByte;
131+
break;
132+
}
133+
// take the chip select high to de-select:
134+
digitalWrite(chipSelectPin, HIGH);
135+
// return the result:
136+
return(result);
137+
}
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+

0 commit comments

Comments
 (0)