Diltech RM Consultant
/
Double_I2C_Sample
Sample code with BluePill STM32F103C8 and Oled SSD1306 and LCD1602 on same connect on DS18B20.
ds18b20-single/README.md@1:27d0c6ee0e55, 2022-05-29 (annotated)
- Committer:
- diltech
- Date:
- Sun May 29 18:40:06 2022 -0400
- Revision:
- 1:27d0c6ee0e55
- Parent:
- 0:47b4bbc994df
Etape1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
diltech | 0:47b4bbc994df | 1 | # DS18B20-single |
diltech | 0:47b4bbc994df | 2 | |
diltech | 0:47b4bbc994df | 3 | An easy to use library to read one single DS18B20 sensor on a 1-wire bus in mbed-os. |
diltech | 0:47b4bbc994df | 4 | |
diltech | 0:47b4bbc994df | 5 | ## Usage |
diltech | 0:47b4bbc994df | 6 | |
diltech | 0:47b4bbc994df | 7 | 1. include Library |
diltech | 0:47b4bbc994df | 8 | ``` |
diltech | 0:47b4bbc994df | 9 | #include "DS18B20.h" |
diltech | 0:47b4bbc994df | 10 | ``` |
diltech | 0:47b4bbc994df | 11 | |
diltech | 0:47b4bbc994df | 12 | 2. Open instance of sensor and pass the PinName to the constructor |
diltech | 0:47b4bbc994df | 13 | ``` |
diltech | 0:47b4bbc994df | 14 | DS18B20 sensor(PinName) //Substitude PinName for the pin to witch the Sensor is connected |
diltech | 0:47b4bbc994df | 15 | ``` |
diltech | 0:47b4bbc994df | 16 | |
diltech | 0:47b4bbc994df | 17 | 3. Read temperature by by using readTemp() |
diltech | 0:47b4bbc994df | 18 | ``` |
diltech | 0:47b4bbc994df | 19 | float temperature = sensor.readTemp(); |
diltech | 0:47b4bbc994df | 20 | ``` |
diltech | 0:47b4bbc994df | 21 | Please note that taking a measurement will take over 750ms |
diltech | 0:47b4bbc994df | 22 | |
diltech | 0:47b4bbc994df | 23 | ## Example |
diltech | 0:47b4bbc994df | 24 | ### Nucleo L432KC with sensor on pin A0 (mbed-os 5) |
diltech | 0:47b4bbc994df | 25 | ``` |
diltech | 0:47b4bbc994df | 26 | #include "mbed.h" |
diltech | 0:47b4bbc994df | 27 | #include "DS18B20.h" //include the library |
diltech | 0:47b4bbc994df | 28 | |
diltech | 0:47b4bbc994df | 29 | DS18B20 sensor(A0); //Create an instance of DS18B20, name it sensor, set A0 as pin |
diltech | 0:47b4bbc994df | 30 | |
diltech | 0:47b4bbc994df | 31 | int main() |
diltech | 0:47b4bbc994df | 32 | { |
diltech | 0:47b4bbc994df | 33 | while(1) |
diltech | 0:47b4bbc994df | 34 | { |
diltech | 0:47b4bbc994df | 35 | printf("Temp: %6.1f C\n", tempsense.readTemp()); //read the temperature and output it to the default debug serial interface. |
diltech | 0:47b4bbc994df | 36 | ThisThread::sleep_for(1000); //wait 1s before measuring again |
diltech | 0:47b4bbc994df | 37 | } |
diltech | 0:47b4bbc994df | 38 | } |
diltech | 0:47b4bbc994df | 39 | ``` |
diltech | 0:47b4bbc994df | 40 | |
diltech | 0:47b4bbc994df | 41 | ### Nucleo L432KC with sensor on pin A0 (mbed-os 6) |
diltech | 0:47b4bbc994df | 42 | 1. main.cpp |
diltech | 0:47b4bbc994df | 43 | ``` |
diltech | 0:47b4bbc994df | 44 | #include "mbed.h" |
diltech | 0:47b4bbc994df | 45 | #include "DS18B20.h" //include the library |
diltech | 0:47b4bbc994df | 46 | |
diltech | 0:47b4bbc994df | 47 | DS18B20 sensor(A0); //Create an instance of DS18B20, name it sensor, set A0 as pin |
diltech | 0:47b4bbc994df | 48 | |
diltech | 0:47b4bbc994df | 49 | int main() |
diltech | 0:47b4bbc994df | 50 | { |
diltech | 0:47b4bbc994df | 51 | while(1) |
diltech | 0:47b4bbc994df | 52 | { |
diltech | 0:47b4bbc994df | 53 | printf("Temp: %6.1f C\n", tempsense.readTemp()); //read the temperature and output it to the default debug serial interface. |
diltech | 0:47b4bbc994df | 54 | ThisThread::sleep_for(1s); //wait 1s before measuring again |
diltech | 0:47b4bbc994df | 55 | } |
diltech | 0:47b4bbc994df | 56 | } |
diltech | 0:47b4bbc994df | 57 | ``` |
diltech | 0:47b4bbc994df | 58 | |
diltech | 0:47b4bbc994df | 59 | 2. Printing floats in mbed-os 6 is disabled by default to save space. If you have a mbed_app.json file in your project root directory, add |
diltech | 0:47b4bbc994df | 60 | ``` |
diltech | 0:47b4bbc994df | 61 | "target_overrides": { |
diltech | 0:47b4bbc994df | 62 | "*": { |
diltech | 0:47b4bbc994df | 63 | "target.printf_lib": "std" |
diltech | 0:47b4bbc994df | 64 | } |
diltech | 0:47b4bbc994df | 65 | } |
diltech | 0:47b4bbc994df | 66 | ``` |
diltech | 0:47b4bbc994df | 67 | to this file. Otherwise, create mbed_app.json in the projects root directory and paste the following code into it: |
diltech | 0:47b4bbc994df | 68 | ``` |
diltech | 0:47b4bbc994df | 69 | { |
diltech | 0:47b4bbc994df | 70 | "target_overrides": { |
diltech | 0:47b4bbc994df | 71 | "*": { |
diltech | 0:47b4bbc994df | 72 | "target.printf_lib": "std" |
diltech | 0:47b4bbc994df | 73 | } |
diltech | 0:47b4bbc994df | 74 | } |
diltech | 0:47b4bbc994df | 75 | } |
diltech | 0:47b4bbc994df | 76 | ``` |
diltech | 0:47b4bbc994df | 77 | |
diltech | 0:47b4bbc994df | 78 | ## Installation |
diltech | 0:47b4bbc994df | 79 | |
diltech | 0:47b4bbc994df | 80 | ### MBED online compiler |
diltech | 0:47b4bbc994df | 81 | 1. Right-Click your project. |
diltech | 0:47b4bbc994df | 82 | 2. select Import "Library" > "From URL". |
diltech | 0:47b4bbc994df | 83 | 3. Type |
diltech | 0:47b4bbc994df | 84 | ``` |
diltech | 0:47b4bbc994df | 85 | https://github.com/LukasGessner/DS18B20-single |
diltech | 0:47b4bbc994df | 86 | ``` |
diltech | 0:47b4bbc994df | 87 | as URL. |
diltech | 0:47b4bbc994df | 88 | |
diltech | 0:47b4bbc994df | 89 | ### MBED Studio |
diltech | 0:47b4bbc994df | 90 | 1. Make shure your target program is selected under "Active program". |
diltech | 0:47b4bbc994df | 91 | 2. On the bottom of the screen, select the "Library" tab. |
diltech | 0:47b4bbc994df | 92 | 3. Click the "+" icon. |
diltech | 0:47b4bbc994df | 93 | 4. Enter |
diltech | 0:47b4bbc994df | 94 | ``` |
diltech | 0:47b4bbc994df | 95 | https://github.com/LukasGessner/DS18B20-single |
diltech | 0:47b4bbc994df | 96 | ``` |
diltech | 0:47b4bbc994df | 97 | as the URL. |
diltech | 0:47b4bbc994df | 98 | |
diltech | 0:47b4bbc994df | 99 | |
diltech | 0:47b4bbc994df | 100 | ### MBED CLI |
diltech | 0:47b4bbc994df | 101 | 1. Open the command prompt in the project folder. |
diltech | 0:47b4bbc994df | 102 | 2. Type |
diltech | 0:47b4bbc994df | 103 | ``` |
diltech | 0:47b4bbc994df | 104 | mbed add https://github.com/LukasGessner/DS18B20-single |
diltech | 0:47b4bbc994df | 105 | ``` |
diltech | 0:47b4bbc994df | 106 | into the command prompt. |
diltech | 0:47b4bbc994df | 107 |