Skip to content

Commit a44d606

Browse files
committed
Update readme.md
1 parent 6d112a6 commit a44d606

File tree

1 file changed

+15
-14
lines changed
  • EnvironmentalSensors/PMS5003_AQI_sensor

1 file changed

+15
-14
lines changed

EnvironmentalSensors/PMS5003_AQI_sensor/readme.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The sensor's [data sheet](https://www.aqmd.gov/docs/default-source/aq-spec/resou
3737
| 31 |Checksum High Byte | variable |
3838
| 32 |Checksum Low Byte | variable |
3939

40-
_ Table 2. Data Bytes of the PMS5003 Sensor Data Format_
40+
_Table 2. Data Bytes of the PMS5003 Sensor Data Format_
4141

4242

4343
> **Note:** in programming contexts, single-byte values are often written in base-16, or *hexadecimal* notation. In this notation, each digit has 16 possible values, 0-9 then A-F. The `0x` at the beginning indicates that a number is hexadecimal. It makes it easy to write each byte as a two-digit figure: 0-255 in decimal notation is 0-FF in hexadcimal. These notes will follow that convention.
@@ -129,14 +129,14 @@ It's possible that one of the data bytes could have the value 0x42, so it's good
129129

130130
All of the data bytes represent 2-byte values. You need to combine each pair of bytes into a single value.
131131

132-
To combine individual bytes into larger values, it helps to imagine those bytes and values as bits in memory. A single byte variable takes up 8 bits in the microcontroller's memory, a two-byte variable takes 16 bits, and a four-byte variable takes 32 bits. Each bit is just a switch in memory (really a transistor) that's turned on or off. Each bit position represents a power of two:
132+
To combine individual bytes into larger values, it helps to imagine those bytes and values as bits in memory. A single byte variable takes up 8 bits in the microcontroller's memory, a two-byte variable takes 16 bits, and a four-byte variable takes 32 bits. Each bit is just a switch in memory (really a transistor) that's turned on or off. Each bit position represents a power of two. Let's take this byte: `00111110`:
133133

134134
| Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
135135
|-|-|-|-|-|-|-|-|
136136
| 2<sup>7</sup> | 2<sup>6</sup> | 2<sup>5</sup> | 2<sup>4</sup> | 2<sup>3</sup> | 2<sup>2</sup> | 2<sup>1</sup> | 2<sup>0</sup> |
137137
|0|0|1|1|1|1|1|0|
138138

139-
The byte above has the value 58.
139+
This byte has the value 58. Here's the breakdown again:
140140

141141
2<sup>5</sup> + 2<sup>4</sup> + 2<sup>3</sup> + 2<sup>2</sup> + 2<sup>1</sup> = \
142142
32 + 16 + 8 + 4 + 2 = \
@@ -149,19 +149,19 @@ Here are three different bytes holding three different values:
149149
01111100 = 124
150150
11111000 = 248
151151
```
152-
Notice how they have the same pattern of 1 and 0 in the middle, but the pattern is shifted one bit position each time. Also notice how, each time you shift one bit to the left, the value doubles. This is called **bit shifting**. It's a common operation in programming. Bit shifting by one position to the left (the symbol for this is `<<`) doubles the value, and by one position to the right (`>>`) halves the value. Looking at those same byte values again:
152+
They all have different values, but notice how they have the same pattern of 1 and 0 in the middle, but the pattern is shifted one bit position each time. Also notice how, each time you shift one bit to the left, the value doubles. This is called **bit shifting**. It's a common operation in programming. Bit shifting by one position to the left (the symbol for this is `<<`) doubles the value, and by one position to the right (`>>`) halves the value. Looking at those same byte values again:
153153

154-
|Bits |Base-10| Equals | Equals|
154+
|Bits |Decimal Value| Equals | Equals|
155155
|--|--|--|--|
156156
|00111110 | 58 | 124 >> 1 | 248 >> 2 |
157157
|01111100 | 124 | 58 << 1 | 248 >> 1 |
158158
| 11111000 | 248 | 58 << 2 | 124 << 1 |
159159

160-
Now, imagine combining two byte values into a single two-byte variable. Declare it as an `int` and call it `c` (space added to make it readable):
160+
Bit shifting can be very helpful in combining multiple bytes into a single value. Imagine `int` variable called `c` (space added to make it readable):
161161
```
162162
int c = 00000000 00000000
163163
```
164-
Let's take that checksum value from above. It was two bytes, `0x02` and `0x24` in hexadecimal. That's `00000010` and `00100100` in binary. To combine them in one byte, first you put the high byte in the variable (`c = 0x02`) and the bits of the variable will look like this:
164+
Let's take that checksum value from above and put it in our variable. It was two bytes, `0x02` and `0x24` in hexadecimal. That's `00000010` and `00100100` in binary. To combine them in one byte, first you put the high byte in the variable (`c = 0x02`) and the bits of the variable will look like this:
165165

166166
```
167167
00000000 00000010
@@ -174,7 +174,8 @@ Then we add the lower byte to the variable (`c = c + 0x24`). Now it will look li
174174
```
175175
00000010 00100100
176176
```
177-
That's how we combine two bytes into one larger variable. Since bit shifting changes the values by powers of two, you can also use multiplication. Whenever you need to combine two bytes into a single value, you can use this formula:
177+
178+
That's how we combine two bytes into one larger variable. Since bit shifting changes the values by powers of two, you can use multiplication instead if you prefer. Bit shifting is basically multiplying by powers of 2. Whenever you need to combine two bytes into a single value, you can use this formula:
178179
```
179180
dataValue = (highByte << 8) + lowByte
180181
```
@@ -221,9 +222,9 @@ You could program the parsing just like that, but it's a lot of typing. In the e
221222
// boil 26 bytes down into 13 data values:
222223
for (int r = 0; r < 13; r++) {
223224
// calculate the actual reading values:
224-
// the variables below are to explain the relationship between
225-
// the two arrays:
226-
const int offset = 3;
225+
// the variables below are to explain
226+
// the relationship between the two arrays:
227+
int offset = 3;
227228
int bufferIndex = (r * 2) + offset;
228229
readings[r] = (buffer[bufferIndex] << 8) + buffer[bufferIndex + 1];
229230
}
@@ -241,13 +242,13 @@ void loop() {
241242
// if you got no data, skip the rest of the loop
242243
// if the second header byte is missing, skip the rest of the loop
243244
// if you got a full buffer (31 bytes), process it and print it
244-
// using the function processData
245+
// using the function processData
245246
}
246247
247248
int processData() {
248-
// if the first byte is not 0x42, return an error
249+
// if the first byte is not 0x42, stop and return an error
249250
// calculate checksum starting with the first byte
250-
// if the checksum is wrong, return an error
251+
// if the checksum is wrong, stop and return an error
251252
// if all is good, continue processing
252253
// process the data length into one value
253254
// boil the next 26 bytes down into 13 data readings

0 commit comments

Comments
 (0)