Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of NUCLEO-F401RE-DS1820andThermistorNTC10K by
main.cpp@4:02a922030bc3, 2016-01-07 (annotated)
- Committer:
- emcu
- Date:
- Thu Jan 07 00:29:30 2016 +0000
- Revision:
- 4:02a922030bc3
- Parent:
- 3:f483abe4bc57
- Child:
- 5:22fb02a92579
NUCLEO-F401RE + DS18B20 + Thermistor; Thermistor (NTC) of 10K - For calculate the temperature we use the Steinhart-Hart equation.;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| emcu | 4:02a922030bc3 | 1 | |
| emcu | 4:02a922030bc3 | 2 | /** |
| emcu | 4:02a922030bc3 | 3 | |
| emcu | 4:02a922030bc3 | 4 | NUCLEO-F401RE + DS18B20 + Thermistor |
| emcu | 4:02a922030bc3 | 5 | Thermistor response, not fully tested. |
| emcu | 4:02a922030bc3 | 6 | |
| emcu | 4:02a922030bc3 | 7 | By: www.emcu.it |
| emcu | 4:02a922030bc3 | 8 | Date: Jan.2015 |
| emcu | 4:02a922030bc3 | 9 | Version: 1.0 |
| emcu | 4:02a922030bc3 | 10 | Name: NUCLEO-F401RE-DS1820andThermistorNTC10K |
| emcu | 4:02a922030bc3 | 11 | NOTE: For more info see here: http://www.emcu.it/NUCLEOevaBoards/mBed/QSG-Mbed-Library.pdf |
| emcu | 4:02a922030bc3 | 12 | |
| emcu | 4:02a922030bc3 | 13 | THE SOFTWARE AND HARDWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| emcu | 4:02a922030bc3 | 14 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| emcu | 4:02a922030bc3 | 15 | FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| emcu | 4:02a922030bc3 | 16 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| emcu | 4:02a922030bc3 | 17 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| emcu | 4:02a922030bc3 | 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| emcu | 4:02a922030bc3 | 19 | |
| emcu | 4:02a922030bc3 | 20 | UART Configuration (It is necessary for see the results, we suggest to use TeraTerm on PC) |
| emcu | 4:02a922030bc3 | 21 | Baud Rate: 9600 |
| emcu | 4:02a922030bc3 | 22 | Data Bit: 8 |
| emcu | 4:02a922030bc3 | 23 | Parity: NONE |
| emcu | 4:02a922030bc3 | 24 | Stop Bit: 1 |
| emcu | 4:02a922030bc3 | 25 | Flow Control: NONE |
| emcu | 4:02a922030bc3 | 26 | |
| emcu | 4:02a922030bc3 | 27 | This SW is ready to use on the NUCLEO-F334R8. |
| emcu | 4:02a922030bc3 | 28 | Connect to the NUCLEO-F334R8 the DS18B20 sensor (see the schematic below). |
| emcu | 4:02a922030bc3 | 29 | The temperature sampling time is 1 sec. |
| emcu | 4:02a922030bc3 | 30 | |
| emcu | 4:02a922030bc3 | 31 | DS18B20 front view |
| emcu | 4:02a922030bc3 | 32 | __________ |
| emcu | 4:02a922030bc3 | 33 | | | |
| emcu | 4:02a922030bc3 | 34 | | DS | |
| emcu | 4:02a922030bc3 | 35 | | 18B20 | |
| emcu | 4:02a922030bc3 | 36 | | | |
| emcu | 4:02a922030bc3 | 37 | |__________| |
| emcu | 4:02a922030bc3 | 38 | | | | |
| emcu | 4:02a922030bc3 | 39 | 1 2 3 |
| emcu | 4:02a922030bc3 | 40 | GND DQ VCC (3V3) |
| emcu | 4:02a922030bc3 | 41 | | | |______________ to VCC (3.3V on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 42 | | | _|_ |
| emcu | 4:02a922030bc3 | 43 | | | | | |
| emcu | 4:02a922030bc3 | 44 | | | | | 4K7 |
| emcu | 4:02a922030bc3 | 45 | | | | | |
| emcu | 4:02a922030bc3 | 46 | | | -|- |
| emcu | 4:02a922030bc3 | 47 | | |___|______________ to A1 (on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 48 | | |
| emcu | 4:02a922030bc3 | 49 | | |
| emcu | 4:02a922030bc3 | 50 | |______________________ to GND (on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 51 | |
| emcu | 4:02a922030bc3 | 52 | This SW is just for only one DS18B20 |
| emcu | 4:02a922030bc3 | 53 | This SW is a derivative of:: https://developer.mbed.org/users/Sissors/code/DS1820_HelloWorld/ |
| emcu | 4:02a922030bc3 | 54 | On the: https://developer.mbed.org/users/Sissors/code/DS1820_HelloWorld/ there is a multi sensor (DS18B20) example. |
| emcu | 4:02a922030bc3 | 55 | |
| emcu | 4:02a922030bc3 | 56 | Thermistor of 10K - For calculate the temperature we use the Steinhart-Hart equation, see here: |
| emcu | 4:02a922030bc3 | 57 | http://www.emcu.it/RaspBerryPi/RaspBerryPi_UK.html#Misura_NTC |
| emcu | 4:02a922030bc3 | 58 | |
| emcu | 4:02a922030bc3 | 59 | |
| emcu | 4:02a922030bc3 | 60 | ___________________ to VCC (3.3V on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 61 | | |
| emcu | 4:02a922030bc3 | 62 | _|_ |
| emcu | 4:02a922030bc3 | 63 | | | |
| emcu | 4:02a922030bc3 | 64 | | | 10K Thermistor (NTC) |
| emcu | 4:02a922030bc3 | 65 | | | |
| emcu | 4:02a922030bc3 | 66 | -|- |
| emcu | 4:02a922030bc3 | 67 | |___________________ to A5 (on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 68 | _|_ | |
| emcu | 4:02a922030bc3 | 69 | | | | |
| emcu | 4:02a922030bc3 | 70 | | | 10K _|_ |
| emcu | 4:02a922030bc3 | 71 | | | ___ 0,47uF |
| emcu | 4:02a922030bc3 | 72 | -|- | |
| emcu | 4:02a922030bc3 | 73 | | | |
| emcu | 4:02a922030bc3 | 74 | |_________|_________ to GND (on the NUCLEO-F401RE) |
| emcu | 4:02a922030bc3 | 75 | |
| emcu | 4:02a922030bc3 | 76 | |
| emcu | 4:02a922030bc3 | 77 | */ |
| emcu | 4:02a922030bc3 | 78 | |
| emcu | 4:02a922030bc3 | 79 | |
| emcu | 4:02a922030bc3 | 80 | |
| Sissors | 0:e069f9f26768 | 81 | #define MULTIPLE_PROBES |
| emcu | 4:02a922030bc3 | 82 | #define DATA_PIN A1 |
| Sissors | 0:e069f9f26768 | 83 | |
| Sissors | 0:e069f9f26768 | 84 | #ifdef MULTIPLE_PROBES |
| Sissors | 0:e069f9f26768 | 85 | |
| Sissors | 0:e069f9f26768 | 86 | #include "mbed.h" |
| Sissors | 0:e069f9f26768 | 87 | #include "DS1820.h" |
| Sissors | 0:e069f9f26768 | 88 | |
| Sissors | 0:e069f9f26768 | 89 | #define MAX_PROBES 16 |
| emcu | 4:02a922030bc3 | 90 | |
| emcu | 4:02a922030bc3 | 91 | float get_temperature(void); |
| emcu | 4:02a922030bc3 | 92 | |
| emcu | 4:02a922030bc3 | 93 | AnalogIn thermistor(A5); // Thermistor |
| emcu | 4:02a922030bc3 | 94 | |
| emcu | 4:02a922030bc3 | 95 | float temp=0; |
| emcu | 4:02a922030bc3 | 96 | float tempArr[100]; |
| emcu | 4:02a922030bc3 | 97 | int n=0; |
| emcu | 4:02a922030bc3 | 98 | |
| Sissors | 0:e069f9f26768 | 99 | DS1820* probe[MAX_PROBES]; |
| Sissors | 0:e069f9f26768 | 100 | |
| Sissors | 0:e069f9f26768 | 101 | int main() { |
| Sissors | 0:e069f9f26768 | 102 | // Initialize the probe array to DS1820 objects |
| Sissors | 0:e069f9f26768 | 103 | int num_devices = 0; |
| Sissors | 0:e069f9f26768 | 104 | while(DS1820::unassignedProbe(DATA_PIN)) { |
| Sissors | 0:e069f9f26768 | 105 | probe[num_devices] = new DS1820(DATA_PIN); |
| Sissors | 0:e069f9f26768 | 106 | num_devices++; |
| Sissors | 0:e069f9f26768 | 107 | if (num_devices == MAX_PROBES) |
| Sissors | 0:e069f9f26768 | 108 | break; |
| Sissors | 0:e069f9f26768 | 109 | } |
| Sissors | 0:e069f9f26768 | 110 | |
| Sissors | 0:e069f9f26768 | 111 | printf("Found %d device(s)\r\n\n", num_devices); |
| emcu | 4:02a922030bc3 | 112 | while(1) |
| emcu | 4:02a922030bc3 | 113 | { |
| Sissors | 0:e069f9f26768 | 114 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
| Sissors | 0:e069f9f26768 | 115 | for (int i = 0; i<num_devices; i++) |
| emcu | 4:02a922030bc3 | 116 | printf("Device %d returns %3.3foC\r\n", i, probe[i]->temperature()); |
| emcu | 4:02a922030bc3 | 117 | |
| emcu | 4:02a922030bc3 | 118 | for(n=0; n<100; n++) |
| emcu | 4:02a922030bc3 | 119 | { |
| emcu | 4:02a922030bc3 | 120 | tempArr[n]=get_temperature(); |
| emcu | 4:02a922030bc3 | 121 | wait_ms(10); |
| emcu | 4:02a922030bc3 | 122 | } |
| emcu | 4:02a922030bc3 | 123 | temp=0; |
| emcu | 4:02a922030bc3 | 124 | for(n=0; n<100; n++) |
| emcu | 4:02a922030bc3 | 125 | temp=temp + tempArr[n]; |
| emcu | 4:02a922030bc3 | 126 | temp=temp/100; |
| emcu | 4:02a922030bc3 | 127 | printf("NTC returns %3.3foC\r\n\n\r", temp); |
| emcu | 4:02a922030bc3 | 128 | |
| Sissors | 0:e069f9f26768 | 129 | wait(1); |
| Sissors | 0:e069f9f26768 | 130 | } |
| Sissors | 0:e069f9f26768 | 131 | |
| Sissors | 0:e069f9f26768 | 132 | } |
| Sissors | 0:e069f9f26768 | 133 | |
| Sissors | 0:e069f9f26768 | 134 | #else |
| Sissors | 0:e069f9f26768 | 135 | #include "mbed.h" |
| Sissors | 0:e069f9f26768 | 136 | #include "DS1820.h" |
| Sissors | 0:e069f9f26768 | 137 | |
| Sissors | 0:e069f9f26768 | 138 | DS1820 probe(DATA_PIN); |
| Sissors | 0:e069f9f26768 | 139 | |
| Sissors | 0:e069f9f26768 | 140 | int main() { |
| Sissors | 0:e069f9f26768 | 141 | while(1) { |
| Sissors | 0:e069f9f26768 | 142 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
| emcu | 4:02a922030bc3 | 143 | printf("It is %3.3foC\r\n", probe.temperature()); |
| Sissors | 0:e069f9f26768 | 144 | wait(1); |
| Sissors | 0:e069f9f26768 | 145 | } |
| Sissors | 0:e069f9f26768 | 146 | } |
| Sissors | 0:e069f9f26768 | 147 | |
| emcu | 4:02a922030bc3 | 148 | #endif |
| emcu | 4:02a922030bc3 | 149 | |
| emcu | 4:02a922030bc3 | 150 | /* |
| emcu | 4:02a922030bc3 | 151 | Thermistor (NTC) of 10K - For calculate the temperature we use the Steinhart-Hart equation, see here: |
| emcu | 4:02a922030bc3 | 152 | http://www.emcu.it/RaspBerryPi/RaspBerryPi_UK.html#Misura_NTC |
| emcu | 4:02a922030bc3 | 153 | ATTENTION: Thermistor response, not fully tested. |
| emcu | 4:02a922030bc3 | 154 | */ |
| emcu | 4:02a922030bc3 | 155 | float get_temperature(void) |
| emcu | 4:02a922030bc3 | 156 | { |
| emcu | 4:02a922030bc3 | 157 | unsigned int a, beta = 3975; |
| emcu | 4:02a922030bc3 | 158 | float temperature, resistance; |
| emcu | 4:02a922030bc3 | 159 | |
| emcu | 4:02a922030bc3 | 160 | a = thermistor.read_u16(); |
| emcu | 4:02a922030bc3 | 161 | |
| emcu | 4:02a922030bc3 | 162 | /* Calculate the resistance of the thermistor from analog votage read. */ |
| emcu | 4:02a922030bc3 | 163 | resistance = (float) 10000.0 * ((65536.0 / a) - 1); |
| emcu | 4:02a922030bc3 | 164 | |
| emcu | 4:02a922030bc3 | 165 | /* Convert the resistance to temperature using Steinhart's Hart equation */ |
| emcu | 4:02a922030bc3 | 166 | temperature=(1/((log(resistance/10000.0)/beta) + (1.0/298.15)))-273.15; |
| emcu | 4:02a922030bc3 | 167 | |
| emcu | 4:02a922030bc3 | 168 | return temperature; |
| emcu | 4:02a922030bc3 | 169 | } |
