|
| 1 | +#include "../../scd4x_i2c.h" |
1 | 2 |
|
| 3 | +#include <hardware/i2c.h> |
| 4 | +#include <pico/stdlib.h> |
| 5 | +#include <pico/time.h> |
2 | 6 |
|
3 |
| -#include "hardware/i2c.h" |
4 |
| -#include "pico/binary_info.h" |
5 |
| -#include "pico/stdlib.h" |
6 |
| -#include "scd4x_i2c.h" |
7 | 7 | #include <stdio.h>
|
8 | 8 |
|
9 |
| -/// I2C address |
10 |
| -static int addr = 0x62; |
11 |
| - |
12 |
| -// I2C Pins |
13 |
| -static uint sda_pin = 16; |
14 |
| -static uint scl_pin = 17; |
15 |
| - |
16 |
| -// This is the main entry for your c application. U |
17 |
| -// is |
18 |
| -int main() { |
19 |
| - |
| 9 | +int main(void) { |
20 | 10 | stdio_init_all();
|
21 | 11 |
|
22 |
| - // Setup I2c using pins 16 & 17 |
23 |
| - i2c_init(i2c_default, 400 * 1000); |
24 |
| - gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); |
25 |
| - gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); |
| 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); |
26 | 15 |
|
27 |
| - // This variable will hold the return status of the function calls. |
28 |
| - // You can separate each function call result into their own variable or re |
29 |
| - // - use this. |
| 16 | + // Setup I2C using GPIO pins 12 & 13. |
| 17 | + const uint desired_clock_hz = 400 * 1000; |
| 18 | + const uint actual_baudrate = i2c_init(i2c_default, desired_clock_hz); |
| 19 | + printf("The I2C baudrate is %u Hz\n", actual_baudrate); |
| 20 | + const uint sda_pin = 12; |
| 21 | + const uint scl_pin = 13; |
| 22 | + gpio_set_function(sda_pin, GPIO_FUNC_I2C); |
| 23 | + gpio_set_function(scl_pin, GPIO_FUNC_I2C); |
| 24 | + gpio_pull_up(sda_pin); |
| 25 | + gpio_pull_up(scl_pin); |
30 | 26 |
|
31 |
| - int status = 0; |
| 27 | + // Initialize driver with i2c address |
| 28 | + scd4x_init(SCD41_I2C_ADDR_62); |
32 | 29 |
|
33 |
| - // Stop any readings if occuring |
34 |
| - status = scd4x_stop_periodic_measurement(); |
| 30 | + int status; |
35 | 31 |
|
36 |
| - // Perform self test |
37 |
| - uint16_t* selfTest = 0; |
38 |
| - scd4x_perform_self_test(selfTest); |
39 |
| - |
40 |
| - // Get Serial number 3 parts |
41 |
| - uint16_t one; |
42 |
| - uint16_t two; |
43 |
| - uint16_t three; |
| 32 | + // Stop any ongoing measurement. |
| 33 | + status = scd4x_stop_periodic_measurement(); |
| 34 | + if (status) { |
| 35 | + printf("Unable to stop measurement. Error: %d\n", status); |
| 36 | + return status; |
| 37 | + } |
44 | 38 |
|
45 |
| - scd4x_get_serial_number(&one, &two, &three); |
| 39 | + // Get serial number. |
| 40 | + uint16_t serial_number[3] = {0}; |
| 41 | + status = scd4x_get_serial_number(serial_number, 3); |
| 42 | + if (status) { |
| 43 | + printf("Unable to get sensor serial number. Error: %d\n", status); |
| 44 | + return status; |
| 45 | + } |
| 46 | + printf("Sensor serial number is: 0x%x 0x%x 0x%x\n", (int)serial_number[0], |
| 47 | + (int)serial_number[1], (int)serial_number[2]); |
46 | 48 |
|
47 | 49 | // Start the readings.
|
48 |
| - status1 = scd4x_start_periodic_measurement(); |
49 |
| - |
50 |
| - while (1) { |
| 50 | + status = scd4x_start_periodic_measurement(); |
| 51 | + if (status) { |
| 52 | + printf("Unable to start periodic measurement. Error %d\n", status); |
| 53 | + return status; |
| 54 | + } |
51 | 55 |
|
52 |
| - // Check if data is ready to read |
| 56 | + for (;;) { |
| 57 | + // Wait for the measurement to complete. |
| 58 | + sleep_ms(5000 - 10); |
53 | 59 | bool dataReady;
|
54 |
| - while (dataReady == false) { |
55 |
| - |
56 |
| - status1 = scd4x_get_data_ready_flag(&dataReady); |
| 60 | + do { |
| 61 | + sleep_ms(10); |
| 62 | + status = scd4x_get_data_ready_status(&dataReady); |
| 63 | + if (status) { |
| 64 | + printf("Unable to get sensor readiness status. Error %d.\n", |
| 65 | + status); |
| 66 | + return status; |
| 67 | + } |
| 68 | + } while (!dataReady); |
| 69 | + |
| 70 | + // Read the measurement data and convert it to common units. |
| 71 | + uint16_t co2Raw; // ppm |
| 72 | + int32_t temperatureRaw; // millicelsius |
| 73 | + int32_t humidityRaw; // millipercent |
| 74 | + status = scd4x_read_measurement(&co2Raw, &temperatureRaw, &humidityRaw); |
| 75 | + if (status) { |
| 76 | + printf("Unable to read measurement data. Error: %d\n", status); |
| 77 | + return status; |
57 | 78 | }
|
58 | 79 |
|
59 |
| - // Get the ticks. The scd4x_read_measurement function is giving |
60 |
| - // incorrect data due to the arthimetic |
61 |
| - uint16_t co2; |
62 |
| - uint16_t temp; |
63 |
| - uint16_t humidity; |
64 |
| - status1 = scd4x_read_measurement_ticks(&co2, &temp, &humidity); |
65 |
| - |
66 |
| - // Arithemtic to change raw data into information |
67 |
| - int tempInCelsius = -45 + 175 * temp / 65536; |
68 |
| - int tempInFarenheit = tempInCelsius * 1.8 + 32; |
69 |
| - int humidityPercent = 100 * humidity / 65536; |
70 |
| - |
71 |
| - // Print results to terminal (output) |
72 |
| - printf("C:%d,T:%d,H:%d", co2, tempInFarenheit, humidityPercent); |
| 80 | + const int co2Ppm = co2Raw; |
| 81 | + const float temperatureCelsius = temperatureRaw / 1000.0f; |
| 82 | + const float temperatureFahrenheit = temperatureCelsius * 1.8f + 32; |
| 83 | + const float humidityPercent = humidityRaw / 1000.0f; |
73 | 84 |
|
74 |
| - // Sleep for 5 seconds. |
75 |
| - sleep_ms(5000); |
| 85 | + printf("CO2: %d ppm, Temperature: %.1f C (%.1f F), Humidity: %.1f%%\n", |
| 86 | + co2Ppm, temperatureCelsius, temperatureFahrenheit, |
| 87 | + humidityPercent); |
76 | 88 | }
|
77 | 89 | }
|
0 commit comments