🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  1. Micro:bit Products
  2. Micro:bit Starter Kit V1

Temperature Sensor with micro:bit

PreviousTilt Sensor with micro:bitNextDC Motor with micro:bit

Select your delivery address

Get detailed temperature readings with the micro:bit

Written By: Cherie Tan

Difficulty

Moderate

Steps

10

Introduction

We can get an estimate of the temperature using the Micro:bit, though what is actually provided is the temperature of the silicon die on its main CPU. But what if you wanted to get even more accurate temperature readings?

In this guide, we will use a DS18B20 temperature sensor module with the Micro:bit. This temperature sensor is able to get readings like 23.50 degree celsius rather than just 23 degree celsius.

Complete this guide to get started with using this very useful component, where it could be used for data logging, gardening assistance and more.

Step 1 The Module

  • Before we begin, let's take a closer look at the DS18B20 Digital Temperature Sensor Module! It has three pins:

    GND: This pin is labelled as '-' on the module, it is also called 'GND'. What is 'GND'? In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.

    Out: The DS18B20 temperature sensor uses the 1-Wire protocol, and only requires a single data line. This is the data out pin where the temperature data will be output. Later, we will connect this to a GPIO pin on the micro:bit.

    + : This is connected to 3.3V on the micro:bit.

  • Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.

Step 2 Connect module to breadboard

Step 3 Connect GND to -

Step 4 Connect 3.3V to +

Step 5 Connect P0 to OUT

Step 6 Install One Wire library

  • We will now need to install the 1-Wire library on the Arduino IDE to program the temperature sensor. Click on Sketch > Include Library > Manage Libraries...

  • Type 'OneWire' in the search box

  • Choose the library 'Onewire by Jim Studt ... '

  • Click on the 'Install' button

  • Once finished, it should state 'Installed' and will have automatically been added to the Arduino IDE

Step 7 Install the Dallas Temperature Library

  • Next, go back to the Library Manager, and type 'dallas temperature' in the search box.

  • Choose 'DallasTemperature by Miles Burton'

  • Click on the 'Install' button

  • Once complete, it should state 'INSTALLED' and will have automatically been added to your Arduino IDE

Step 8 The code

#include  //includes the library OneWire
#include  //includes the library DallasTemperature

#define ONE_WIRE_BUS 0 // Temperature sensor's data wire is plugged into pin 0 of micro:bit
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float celsius, fahrenheit; //Declare 2 floating point variables

void setup(void)
{
 // start serial port
 Serial.begin(9600);
 Serial.println("Temperature Monitor");
 // Start up the sensor library
 sensors.begin();
}
void loop(void)
{

 Serial.print(" Reading temperature...");
 sensors.requestTemperatures(); // Send the command to get temperature reading
 celsius = sensors.getTempCByIndex(ONE_WIRE_BUS); // Put the reading into a variable
 fahrenheit = celsius * 1.8 + 32.0; //convert to f
 Serial.println("COMPLETE");

 Serial.print("Temperature is: ");
 Serial.print(celsius);
 Serial.print(" Celsius, ");
 Serial.print(fahrenheit);
 Serial.print(" Fahrenheit");

 delay(1000); //waits for 1 second
}
  • First, we include the OneWire and DallasTemperature libraries

  • We then use #define ONE_WIRE_BUS 0 as the temperature sensor's data wire is plugged into pin 0 of micro:bit

  • To send the command that will get the temperature reading, we use: sensors.requestTemperatures();

  • To get the temperature in degree Celsius, we use: sensors.getTempCByIndex(ONE_WIRE_BUS);

  • To convert the temperature to fahrenheit, we take the value of celsius * 1.8 + 32.0

  • To print the temperature readings in degree celsius and fahrenheit:

    • Serial.print(celsius);

    • Serial.print(fahrenheit);

  • Copy this code and upload it to your Arduino using the Arduino IDE.

Step 9 Upload the code

  • To upload the sketch to the micro:bit, click on the tick icon on the top left corner of Arduino IDE to verify the code

  • Then click on the upload icon next to it.

Step 10 Serial Monitor

  • Now that the program has been uploaded to the micro:bit, let's take a look at the temperature readings. You can do so by opening the serial monitor in the Arduino IDE: Click on Tools > Serial Monitor

  • Please make sure the baud rate is set to 9600 in the serial monitor (this can be set at the bottom right-hand corner of monitor)

Mark

Dash icon
Steps icon

We will use the Arduino IDE to program the Micro:bit. If you have not already got the Arduino IDE installed, please follow our previous guide on .

Then, we declare two , 'celsius' and 'fahrenheit'

Serial.begin() is used so that the micro:bit can communicate with the computer. We use a rate of 9600, although there are other rates you could use too. You can read more about it .

© 2023 Little Bird Electronics Pty Ltd. Made with ❤️ in SYD. All prices inc GST. ABN 15 634 521 449. We're .

how to set up the Arduino IDE for micro:bit
variables
here
🐥 @lbhq on Twitter