77 lines
2.2 KiB
C++
77 lines
2.2 KiB
C++
#include <Arduino.h>
|
|
|
|
// Include the libraries we need
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
// Data wire is plugged into port 2 on the Pico
|
|
#define ONE_WIRE_BUS 17
|
|
|
|
// External timer pin
|
|
const int inputPin = 21;
|
|
|
|
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
// Pass our oneWire reference to Dallas Temperature.
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
/*
|
|
* The setup function. We only start the sensors here
|
|
*/
|
|
void setup(void)
|
|
{
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
// start serial port
|
|
Serial.begin(9600);
|
|
Serial.println("Dallas Temperature IC Control Library Demo");
|
|
|
|
// Start up the library
|
|
sensors.begin();
|
|
|
|
// Configure the timing pin as an input with an internal pull-down resistor.
|
|
// This ensures the pin reads LOW when no signal is present.
|
|
pinMode(inputPin, INPUT_PULLDOWN);
|
|
}
|
|
|
|
/*
|
|
* Main function, get and show the temperature
|
|
*/
|
|
void loop(void)
|
|
{
|
|
// Read the state of the pin
|
|
int sensorValue = digitalRead(inputPin);
|
|
|
|
// Check if the pin is HIGH
|
|
if (sensorValue == HIGH) {
|
|
Serial.println("GPIO 21 is HIGH");
|
|
// Add your HIGH-state logic here
|
|
} else {
|
|
Serial.println("GPIO 21 is LOW");
|
|
}
|
|
|
|
// Small delay to prevent flooding the serial monitor
|
|
delay(100);
|
|
|
|
// call sensors.requestTemperatures() to issue a global temperature
|
|
// request to all devices on the bus
|
|
Serial.print("Requesting temperatures...");
|
|
sensors.requestTemperatures(); // Send the command to get temperatures
|
|
Serial.println("DONE");
|
|
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
|
|
delay(1500);
|
|
digitalWrite(LED_BUILTIN, LOW); // turn the LED off (HIGH is the voltage level)
|
|
// After we got the temperatures, we can print them here.
|
|
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
|
|
float tempC = sensors.getTempCByIndex(0);
|
|
// Check if reading was successful
|
|
if (tempC != DEVICE_DISCONNECTED_C)
|
|
{
|
|
Serial.print("Temperature for the device 1 (index 0) is: ");
|
|
Serial.println(tempC);
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Error: Could not read temperature data");
|
|
}
|
|
} |