Introduction
Compared with an analog temperature sensor, a digital temperature sensor (as shown below) only adds a digital output. You can adjust the threshold by the potentiometer onside. When the output is higher than the threshold, the sensor will output high level; when it is lower than the threshold, the sensor will output low level.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 1 * Digital Temperature Sensor module
– Several jumper wires
Experimental Principle
In this experiment, we will use a Digital-temperature Sensor module and an LED attached to pin 13 of SunFounder to build a simple circuit to make a temperature light.
With the LED attached to pin 13, connect the pin DO to D7 of SunFounder Uno. When the Digital Temperature Sensor detects that the ambient temperature is higher than a certain value (threshold), the LED will be on. Otherwise, it will be off. You can adjust the threshold by adjusting the potentiometer.
Experimental Procedures
Step 1: Build the circuit
Digital Temperature Sensor Module SunFounder Uno
AO —————————————- A0
DO —————————————- D7
– —————————————- GND
+ —————————————– 5V
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)
Step 3: Compile
Step 4: Upload the sketch to SunFounder Uno
Now, touch the thermistor and you will see the LED attached to pin 13 on SunFounder Uno board light up after a while.
Code
const int buttonPin = 7; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin// variables will change: int buttonState = 0; // variable for reading the pushbutton statusvoid setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); Serial.begin(9600); }void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); int val = analogRead(0); Serial.println(val); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } } |