Skip to content

Commit 184ed70

Browse files
committed
Modified Barometric Pressure Sensor to make it easier for beginners to understand.
(Filtered from arduino/Arduino@d58ea9b)
1 parent 5984e7e commit 184ed70

File tree

1 file changed

+103
-102
lines changed

1 file changed

+103
-102
lines changed

examples/BarometricPressureSensor/BarometricPressureSensor.pde

Lines changed: 103 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,143 @@
1-
21
/*
32
SCP1000 Barometric Pressure Sensor Display
4-
5-
Shows the output of a Barometric Pressure Sensor on a
6-
Uses the SPI library. For details on the sensor, see:
7-
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
8-
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
9-
10-
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
11-
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
12-
13-
Circuit:
14-
SCP1000 sensor attached to pins 6, 7, 10 - 13:
15-
DRDY: pin 6
16-
CSB: pin 7
17-
MOSI: pin 11
18-
MISO: pin 12
19-
SCK: pin 13
20-
21-
created 31 July 2010
22-
modified 14 August 2010
23-
by Tom Igoe
24-
*/
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+
modified 14 August 2010
22+
by Tom Igoe
23+
*/
2524

2625
// the sensor communicates using SPI, so include the library:
2726
#include <SPI.h>
2827

2928
//Sensor's memory register addresses:
30-
const int PRESSURE = 0x1F; // 3 most significant bits of pressure
31-
const int PRESSURE_LSB = 0x20; // 16 least significant bits of pressure
32-
const int TEMPERATURE = 0x21; // 16 bit temperature reading
33-
cont byte READ = 0b11111100; // SCP1000's read command
29+
const int PRESSURE = 0x1F; //3 most significant bits of pressure
30+
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
31+
const int TEMPERATURE = 0x21; //16 bit temperature reading
32+
const byte READ = 0b11111100; // SCP1000's read command
3433
const byte WRITE = 0b00000010; // SCP1000's write command
3534

3635
// pins used for the connection with the sensor
3736
// the other you need are controlled by the SPI library):
38-
const int dataReadyPin = 6;
37+
const int dataReadyPin = 6;
3938
const int chipSelectPin = 7;
4039

4140
void setup() {
42-
Serial.begin(9600);
41+
Serial.begin(9600);
4342

44-
// start the SPI library:
45-
SPI.begin();
43+
// start the SPI library:
44+
SPI.begin();
4645

47-
// initalize the data ready and chip select pins:
48-
pinMode(dataReadyPin, INPUT);
49-
pinMode(chipSelectPin, OUTPUT);
46+
// initalize the data ready and chip select pins:
47+
pinMode(dataReadyPin, INPUT);
48+
pinMode(chipSelectPin, OUTPUT);
5049

51-
//Configure SCP1000 for low noise configuration:
52-
writeRegister(0x02, 0x2D);
53-
writeRegister(0x01, 0x03);
54-
writeRegister(0x03, 0x02);
55-
// give the sensor time to set up:
56-
delay(100);
50+
//Configure SCP1000 for low noise configuration:
51+
writeRegister(0x02, 0x2D);
52+
writeRegister(0x01, 0x03);
53+
writeRegister(0x03, 0x02);
54+
// give the sensor time to set up:
55+
delay(100);
5756
}
5857

5958
void loop() {
60-
//Select High Resolution Mode
61-
writeRegister(0x03, 0x0A);
59+
//Select High Resolution Mode
60+
writeRegister(0x03, 0x0A);
6261

63-
// don't do anything until the data ready pin is high:
64-
if (digitalRead(dataReadyPin) == HIGH) {
65-
//Read the temperature data
66-
int tempData = readRegister(0x21, 2);
62+
// don't do anything until the data ready pin is high:
63+
if (digitalRead(dataReadyPin) == HIGH) {
64+
//Read the temperature data
65+
int tempData = readRegister(0x21, 2);
6766

68-
// convert the temperature to celsius and display it:
69-
float realTemp = (float)tempData / 20.0;
70-
Serial.print("Temp[C]=");
71-
Serial.print(realTemp);
67+
// convert the temperature to celsius and display it:
68+
float realTemp = (float)tempData / 20.0;
69+
Serial.print("Temp[C]=");
70+
Serial.print(realTemp);
7271

7372

74-
//Read the pressure data highest 3 bits:
75-
byte pressure_data_high = readRegister(0x1F, 1);
76-
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
73+
//Read the pressure data highest 3 bits:
74+
byte pressure_data_high = readRegister(0x1F, 1);
75+
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
7776

78-
//Read the pressure data lower 16 bits:
79-
unsigned int pressure_data_low = readRegister(0x20, 2);
80-
//combine the two parts into one 19-bit number:
81-
long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
77+
//Read the pressure data lower 16 bits:
78+
unsigned int pressure_data_low = readRegister(0x20, 2);
79+
//combine the two parts into one 19-bit number:
80+
long pressure = ((pressure_data_high << 16) | pressure_data_low)/4;
8281

83-
// display the temperature:
84-
Serial.println("\tPressure [Pa]=" + String(pressure));
85-
}
82+
// display the temperature:
83+
Serial.println("\tPressure [Pa]=" + String(pressure));
84+
}
8685
}
8786

8887
//Read from or write to register from the SCP1000:
8988
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
90-
byte inByte = 0; // incoming byte from the SPI
91-
unsigned int result = 0; // result to return
92-
93-
// SCP1000 expects the register name in the upper 6 bits
94-
// of the byte. So shift the bits left by two bits:
95-
thisRegister = thisRegister << 2;
96-
// now combine the address and the command into one byte
97-
dataToSend = thisRegister & READ;
98-
99-
// take the chip select low to select the device:
100-
digitalWrite(chipSelectPin, LOW);
101-
// send the device the register you want to read:
102-
SPI.transfer(dataToSend);
103-
// send a value of 0 to read the first byte returned:
104-
result = SPI.transfer(0x00);
105-
// decrement the number of bytes left to read:
106-
bytesToRead--;
107-
// if you still have another byte to read:
108-
if (bytesToRead > 0) {
109-
// shift the first byte left, then get the second byte:
110-
result = result << 8;
111-
inByte = SPI.transfer(0x00);
112-
// combine the byte you just got with the previous one:
113-
result = result | inByte;
114-
// decrement the number of bytes left to read:
115-
bytesToRead--;
116-
}
117-
// take the chip select high to de-select:
118-
digitalWrite(chipSelectPin, HIGH);
119-
// return the result:
120-
return(result);
89+
byte inByte = 0; // incoming byte from the SPI
90+
unsigned int result = 0; // result to return
91+
Serial.print(thisRegister, BIN);
92+
Serial.print("\t");
93+
// SCP1000 expects the register name in the upper 6 bits
94+
// of the byte. So shift the bits left by two bits:
95+
thisRegister = thisRegister << 2;
96+
// now combine the address and the command into one byte
97+
byte dataToSend = thisRegister & READ;
98+
Serial.println(thisRegister, BIN);
99+
// take the chip select low to select the device:
100+
digitalWrite(chipSelectPin, LOW);
101+
// send the device the register you want to read:
102+
SPI.transfer(dataToSend);
103+
// send a value of 0 to read the first byte returned:
104+
result = SPI.transfer(0x00);
105+
// decrement the number of bytes left to read:
106+
bytesToRead--;
107+
// if you still have another byte to read:
108+
if (bytesToRead > 0) {
109+
// shift the first byte left, then get the second byte:
110+
result = result << 8;
111+
inByte = SPI.transfer(0x00);
112+
// combine the byte you just got with the previous one:
113+
result = result | inByte;
114+
// decrement the number of bytes left to read:
115+
bytesToRead--;
116+
}
117+
// take the chip select high to de-select:
118+
digitalWrite(chipSelectPin, HIGH);
119+
// return the result:
120+
return(result);
121121
}
122122

123123

124124
//Sends a write command to SCP1000
125125

126126
void writeRegister(byte thisRegister, byte thisValue) {
127127

128-
// SCP1000 expects the register address in the upper 6 bits
129-
// of the byte. So shift the bits left by two bits:
130-
thisRegister = thisRegister << 2;
131-
// now combine the register address and the command into one byte:
132-
dataToSend = thisRegister | WRITE;
128+
// SCP1000 expects the register address in the upper 6 bits
129+
// of the byte. So shift the bits left by two bits:
130+
thisRegister = thisRegister << 2;
131+
// now combine the register address and the command into one byte:
132+
byte dataToSend = thisRegister | WRITE;
133+
134+
// take the chip select low to select the device:
135+
digitalWrite(chipSelectPin, LOW);
133136

134-
// take the chip select low to select the device:
135-
digitalWrite(chipSelectPin, LOW);
137+
SPI.transfer(dataToSend); //Send register location
138+
SPI.transfer(thisValue); //Send value to record into register
136139

137-
SPI.transfer(dataToSend); //Send register location
138-
SPI.transfer(thisValue); //Send value to record into register
140+
// take the chip select high to de-select:
141+
digitalWrite(chipSelectPin, HIGH);
142+
}
139143

140-
// take the chip select high to de-select:
141-
digitalWrite(chipSelectPin, HIGH);
142-
}

0 commit comments

Comments
 (0)