Andrew Staples / Mbed OS 5_Light_and_Temp
Committer:
10782824
Date:
Fri Dec 04 15:50:51 2020 +0000
Revision:
0:339d1e10c98b
Thermister, DR and POT included;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
10782824 0:339d1e10c98b 1 /* mbed Microcontroller Library
10782824 0:339d1e10c98b 2 * Copyright (c) 2019 ARM Limited
10782824 0:339d1e10c98b 3 * SPDX-License-Identifier: Apache-2.0
10782824 0:339d1e10c98b 4 */
10782824 0:339d1e10c98b 5
10782824 0:339d1e10c98b 6 #include "mbed.h"
10782824 0:339d1e10c98b 7 #include "platform/mbed_thread.h"
10782824 0:339d1e10c98b 8 #include "stdio.h"
10782824 0:339d1e10c98b 9 /* Reference resistor in series with the thermistor is of 10 KOhm */
10782824 0:339d1e10c98b 10 #define R_REFERENCE (float)(10000)
10782824 0:339d1e10c98b 11 /* Beta constant of this thermistor is 3380 Kelvin. See the thermistor
10782824 0:339d1e10c98b 12 (NCP18XH103F03RB) data sheet for more details. */
10782824 0:339d1e10c98b 13 #define B_CONSTANT (float)(3380)
10782824 0:339d1e10c98b 14 /* Resistance of the thermistor is 10K at 25 degrees C (from data sheet)
10782824 0:339d1e10c98b 15 Therefore R0 = 10000 Ohm, and T0 = 298.15 Kelvin, which gives
10782824 0:339d1e10c98b 16 R_INFINITY = R0 e^(-B_CONSTANT / T0) = 0.1192855 */
10782824 0:339d1e10c98b 17 #define R_INFINITY (float)(0.1192855)
10782824 0:339d1e10c98b 18 /* Zero Kelvin in degree C */
10782824 0:339d1e10c98b 19 #define ABSOLUTE_ZERO (float)(-273.15)
10782824 0:339d1e10c98b 20 //static DigitalIn thermVDD(P10_0); // if wing is detached and powered from 3.3v
10782824 0:339d1e10c98b 21 //static DigitalIn thermGND(P10_3); // don't need to control power to thermistor
10782824 0:339d1e10c98b 22
10782824 0:339d1e10c98b 23
10782824 0:339d1e10c98b 24 // Blinking rate in milliseconds
10782824 0:339d1e10c98b 25 #define BLINKING_RATE_MS 500
10782824 0:339d1e10c98b 26 // #define SW2 P0_4
10782824 0:339d1e10c98b 27 // #define SW3 P12_3
10782824 0:339d1e10c98b 28
10782824 0:339d1e10c98b 29 char buffer[80];
10782824 0:339d1e10c98b 30 /* prototype of function */
10782824 0:339d1e10c98b 31 void displayAt( int x, int y, char *buffer );
10782824 0:339d1e10c98b 32
10782824 0:339d1e10c98b 33 int main()
10782824 0:339d1e10c98b 34 {
10782824 0:339d1e10c98b 35 // Initialise the digital pin LED1 as an output
10782824 0:339d1e10c98b 36 // DigitalOut led(LED1);
10782824 0:339d1e10c98b 37 // DigitalIn pushButton(SW2, PullUp);
10782824 0:339d1e10c98b 38 AnalogIn vTherm(P10_1); //Input pin of Thermister potential divider
10782824 0:339d1e10c98b 39 AnalogIn lightLevel(P10_4); //
10782824 0:339d1e10c98b 40 DigitalOut redLed2 (P10_5);
10782824 0:339d1e10c98b 41 DigitalOut greenLed (P10_0);
10782824 0:339d1e10c98b 42 DigitalOut yellowLed (P0_5);
10782824 0:339d1e10c98b 43 DigitalIn switch3 (P12_3);
10782824 0:339d1e10c98b 44 AnalogIn pot (P10_2);
10782824 0:339d1e10c98b 45
10782824 0:339d1e10c98b 46 printf("\033[2J\033[H"); // clear screen and move the cursor to 0, 0
10782824 0:339d1e10c98b 47 printf("\033[?25l"); // Turn off visible cursor
10782824 0:339d1e10c98b 48 printf("Environmental Control System");
10782824 0:339d1e10c98b 49 // printf( "\033[34m" );
10782824 0:339d1e10c98b 50 fflush(stdout); // send the codes to the terminal
10782824 0:339d1e10c98b 51
10782824 0:339d1e10c98b 52 while (true) {
10782824 0:339d1e10c98b 53 if (switch3 == 1)
10782824 0:339d1e10c98b 54 redLed2 = !redLed2;
10782824 0:339d1e10c98b 55
10782824 0:339d1e10c98b 56 float potValue = pot.read();
10782824 0:339d1e10c98b 57 if (potValue < 0.3)
10782824 0:339d1e10c98b 58 greenLed = !greenLed;
10782824 0:339d1e10c98b 59
10782824 0:339d1e10c98b 60 if (potValue > 0.7)
10782824 0:339d1e10c98b 61 yellowLed = !yellowLed;
10782824 0:339d1e10c98b 62 // thread_sleep_for(BLINKING_RATE_MS);
10782824 0:339d1e10c98b 63 /* if (pushButton == 0) {
10782824 0:339d1e10c98b 64 led = !led;
10782824 0:339d1e10c98b 65 // read thermistor Voltage
10782824 0:339d1e10c98b 66 float refVoltage = vTherm.read() * 2.4; // Range of ADC 0->2*Vref
10782824 0:339d1e10c98b 67 float refCurrent = refVoltage / 10000.0; // 10k Reference Resistor
10782824 0:339d1e10c98b 68 float thermVoltage = 3.3 - refVoltage; // Assume supply voltage is 3.3v
10782824 0:339d1e10c98b 69 float thermResistance = thermVoltage / refCurrent;
10782824 0:339d1e10c98b 70 float logrT = (float32_t)log((float64_t)thermResistance);
10782824 0:339d1e10c98b 71
10782824 0:339d1e10c98b 72 // Calculate temperature from the resistance of thermistor using Steinhart-Hart Equation
10782824 0:339d1e10c98b 73 float stEqn = (float32_t)((0.0009032679) + ((0.000248772) * logrT) +
10782824 0:339d1e10c98b 74 ((2.041094E-07) * pow((float64)logrT, (float32)3)));
10782824 0:339d1e10c98b 75
10782824 0:339d1e10c98b 76 float temperatureC = (float32_t)(((1.0 / stEqn) - 273.15) + 0.5);
10782824 0:339d1e10c98b 77
10782824 0:339d1e10c98b 78 sprintf(buffer, "Temperature is %2.1f\r\n", temperatureC);
10782824 0:339d1e10c98b 79 displayAt(0, 3, buffer);
10782824 0:339d1e10c98b 80
10782824 0:339d1e10c98b 81 float lightPercent = ( 1 - lightLevel.read()) * 100;
10782824 0:339d1e10c98b 82 sprintf( buffer, "Ambient Light is: %3.1f", lightPercent );
10782824 0:339d1e10c98b 83 displayAt(1, 4, buffer);
10782824 0:339d1e10c98b 84 thread_sleep_for(BLINKING_RATE_MS); */
10782824 0:339d1e10c98b 85 }
10782824 0:339d1e10c98b 86 }
10782824 0:339d1e10c98b 87
10782824 0:339d1e10c98b 88 void displayAt( int x, int y, char *buffer ) {
10782824 0:339d1e10c98b 89 printf( "\033[%d;%dH%s", y, x, buffer);
10782824 0:339d1e10c98b 90 fflush(stdout);
10782824 0:339d1e10c98b 91 }