You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: EnvironmentalSensors/PMS5003_AQI_sensor/readme.md
+15-14Lines changed: 15 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ The sensor's [data sheet](https://www.aqmd.gov/docs/default-source/aq-spec/resou
37
37
| 31 |Checksum High Byte | variable |
38
38
| 32 |Checksum Low Byte | variable |
39
39
40
-
_ Table 2. Data Bytes of the PMS5003 Sensor Data Format_
40
+
_Table 2. Data Bytes of the PMS5003 Sensor Data Format_
41
41
42
42
43
43
> **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
129
129
130
130
All of the data bytes represent 2-byte values. You need to combine each pair of bytes into a single value.
131
131
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`:
133
133
134
134
| Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
@@ -149,19 +149,19 @@ Here are three different bytes holding three different values:
149
149
01111100 = 124
150
150
11111000 = 248
151
151
```
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:
153
153
154
-
|Bits |Base-10| Equals | Equals|
154
+
|Bits |Decimal Value| Equals | Equals|
155
155
|--|--|--|--|
156
156
|00111110 | 58 | 124 >> 1 | 248 >> 2 |
157
157
|01111100 | 124 | 58 << 1 | 248 >> 1 |
158
158
| 11111000 | 248 | 58 << 2 | 124 << 1 |
159
159
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):
161
161
```
162
162
int c = 00000000 00000000
163
163
```
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:
165
165
166
166
```
167
167
00000000 00000010
@@ -174,7 +174,8 @@ Then we add the lower byte to the variable (`c = c + 0x24`). Now it will look li
174
174
```
175
175
00000010 00100100
176
176
```
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:
178
179
```
179
180
dataValue = (highByte << 8) + lowByte
180
181
```
@@ -221,9 +222,9 @@ You could program the parsing just like that, but it's a lot of typing. In the e
221
222
// boil 26 bytes down into 13 data values:
222
223
for (int r = 0; r < 13; r++) {
223
224
// calculate the actual reading values:
224
-
// the variables below are to explain the relationship between
0 commit comments