diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..6186d55fbdf7e9b44ae5d2a109c48bc16256ea29 --- /dev/null +++ b/main.c @@ -0,0 +1,54 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> +#include "pico/stdlib.h" +#include "hardware/gpio.h" +#include "hardware/adc.h" +#include "lib/fonts.h" +#include "lib/st7735.h" + +//this task covers how to use the on-board temperature sensor in the RP2040 chip on the Raspberry pi Pico +//it uses the analogue to digital converter (ADC) in the Pico to read the internal temperature of the RP2040 and +//does the necessary data conversion to output the temperature of the chip + +//also covers how to perform the required data processing to take the raw ADC output and convert it +//into a voltage and then into temperature + +//the temperature sensor might not be calibrated so temperatures might differ in different +//boards due to manufacturing inconsistencies +//as such, if you will be using this in a serious project that requires an accurate temperature value, +//I would recommend using an external sensor. + +int main() { + + stdio_init_all(); // Initialise serial in/output + + //Configure ADC + adc_init(); + adc_set_temp_sensor_enabled(true); //Enable temperature sensor + adc_select_input(4); //Select fifth channel of the adc as the input + + sleep_ms(1000); // Wait 1 sec + ST7735_Init(); // Initialise the screen + + //Update temperature display every second + //Infinite loop + while(1) { + uint16_t raw = adc_read(); //Define a variable to store the raw output value from the adc + + //Do the conversion + const float conversion = 3.3f / (1<<12); + float voltage = raw * conversion; // convert raw value to voltage + float temperature = 27 - (voltage - 0.706) / 0.001721; // convert voltage to temperature value + + char strArray[25]; + sprintf(strArray, "Temperature: %d", (int) temperature); //store output on char buffer + + ST7735_FillScreen(ST7735_BLACK);//set screen color + ST7735_WriteString(0, 0, strArray, Font_16x26, ST7735_GREEN, ST7735_BLACK);//display temperature on screen + + //can also use any other function from the library to display the temperature differently + + //wait 1 sec + sleep_ms(1000); + }