Skip to content

Commit 1f6e281

Browse files
dgoffredoLeonieFierz
authored andcommitted
pico example: test on a real board and add documentation
1 parent c6f2f5e commit 1f6e281

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
SCD4x on the Raspberry Pi Pico
2+
==============================
3+
<img alt="Pico W with an SCD41 connected" src="hardware.jpg" height="400"/>
4+
5+
In this example, an SCD41 is connected to a Raspberry Pi Pico W on GPIO pins 16
6+
(I2C0 SDA) and 17 (I2C0 SCL). The same code will work for any variant of the
7+
Pico.
8+
9+
The following shell session is from an Ubuntu machine that had the Pico plugged
10+
in to USB in boot select mode.
11+
12+
```console
13+
$ ls
14+
CMakeLists.txt hardware.jpg main.c README.md sensirion_i2c_hal.c
15+
16+
$ mkdir build
17+
18+
$ cd build
19+
20+
$ cmake -DPICO_SDK_PATH=$HOME/src/pico-sdk ..
21+
[...]
22+
23+
$ make -j
24+
[...]
25+
26+
$ ls
27+
CMakeCache.txt cmake_install.cmake generated main.dis main.elf.map main.uf2 pico-sdk
28+
CMakeFiles elf2uf2 main.bin main.elf main.hex Makefile pioasm
29+
30+
$ cp main.uf2 /media/$USER/RPI-RP2
31+
32+
$ screen /dev/ttyACM0
33+
The I2C baudrate is 399361 Hz
34+
Sensor serial number is: 0x8a7e 0xbb07 0x3ba0
35+
CO2: 1111 ppm, Temperature: 28.4 C (83.1 F), Humidity: 60.4%
36+
CO2: 1080 ppm, Temperature: 28.5 C (83.2 F), Humidity: 61.1%
37+
CO2: 1070 ppm, Temperature: 28.4 C (83.1 F), Humidity: 61.7%
38+
[...]
39+
```
Loading

sample-implementations/RaspberryPi_Pico/main.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
#include "hardware/i2c.h"
2-
#include "pico/binary_info.h"
3-
#include "pico/stdlib.h"
41
#include "../../scd4x_i2c.h"
2+
3+
#include <hardware/i2c.h>
4+
#include <pico/stdlib.h>
5+
#include <pico/time.h>
6+
57
#include <stdio.h>
68

79
int main(void) {
810
stdio_init_all();
911

10-
// Setup I2C using GPIO pins 16 & 17.
12+
// Give us a few seconds to start viewing the output if we're plugged into
13+
// the computer over USB.
14+
sleep_ms(3000);
15+
16+
// Setup I2C using GPIO pins 12 & 13.
1117
const uint desired_clock_hz = 400 * 1000;
1218
const uint actual_baudrate = i2c_init(i2c_default, desired_clock_hz);
1319
printf("The I2C baudrate is %u Hz\n", actual_baudrate);
14-
const uint sda_pin = 16;
15-
const uint scl_pin = 17;
20+
const uint sda_pin = 12;
21+
const uint scl_pin = 13;
1622
gpio_set_function(sda_pin, GPIO_FUNC_I2C);
1723
gpio_set_function(scl_pin, GPIO_FUNC_I2C);
1824
gpio_pull_up(sda_pin);
@@ -73,7 +79,7 @@ int main(void) {
7379
const float temperatureFahrenheit = temperatureCelsius * 1.8f + 32;
7480
const float humidityPercent = humidityRaw / 1000.0f;
7581

76-
printf("CO2: %d ppm, Temperature: %.1f°C (%.1f°F), Humidity: %.1f%%\n",
82+
printf("CO2: %d ppm, Temperature: %.1f C (%.1f F), Humidity: %.1f%%\n",
7783
co2Ppm, temperatureCelsius, temperatureFahrenheit, humidityPercent);
7884
}
7985
}

0 commit comments

Comments
 (0)