7024| 0
|
[进阶] Arduino 和热敏电阻(NTC)测温,并以RGB灯来表示 |
本案例用Arduino 连接NTC热敏电阻和RGB灯来感知温度并以RGB LED灯颜色变化来表达。 NTC热敏电阻是电阻值随温度变化的半导体传感器,其典型特点是阻值对温度非常敏感,在不同的温度下会表现出不同的电阻值,从而根据表现的电阻值可逆推导得到其所处的环境温度值。具有灵敏度高、体积小、热容量小、响应速度快、NTC热敏电阻价格低廉,但是想要得到很高的测量精度,需要做很多优化工作,难度较大。 按照温度系数不同,可分为正温度系数热敏电阻(PTC)、负温度系数热敏电阻(NTC)和临界负温度系数热敏电阻(CTR)。PTC随着温度升高,表现出的电阻值越大; NTC随着温度升高,表现出的电阻值越低;CTR具有负电阻突变特性,在某一温度下,电阻值随温度的增加急剧减小,具有很大的负温度系数。由于具有不同的特性,热敏电阻的用途也是不同的。PTC一般用作加热元件和过热保护;NTC一般用于温度测量和温度补偿;CTR一般用于温控报警等应用。 NTC的测温范围为-60~+300℃,标称阻值一般在1Ω至100MΩ之间,采用精密电阻和热敏电阻组合可扩大测量温度线性范围。图1为NTC实物图,图中所示的为NTC 10D-9和NTC 5D-7。NTC表示为负温度系数的热敏电阻,10D-9和5D-7代表其型号,10D-9代表了常温(25摄氏度)阻值10欧姆,直径9毫米,5D-7代表了常温(25摄氏度)阻值5欧姆,直径7毫米。 除了图1所示的形状之外,热敏电阻制成的探头有珠状、棒杆状、片状和薄膜等,封装外壳有玻璃、镍和不锈钢管等套管结构,如图2所示。 RGB灯是以三原色共同交集成像 连线图 原件是attiny 85, RGB led 10k电阻 thermister 10k 10uF电容 测试 [mw_shl_code=applescript,true]/* made by gada888 x-----------------/\/\/\/\/\/\/\/---------------x---/\/\/\/\/\/\/\----xGND | NTC 10K Thermistor | 10K 1% Resistor | | | ATTINY85 / ARDUINO | | +-\/-+ | | Ain0 (D 5) PB5 1| |8 Vcc | x--- Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1 --- (Blue)- Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1 (Green) ----------|<---x | GND 4| |5 PB0 (D 0) pwm0 (Red) ------------|<---x | +----+ | 100 Ohm x----------------------------------------------------------------|<---x---/\/\/\/\/---Vcc 3.3V */ #include <avr/sleep.h> #include <avr/power.h> #include <avr/wdt.h> boolean flag_wdt = 1; int pinT = PB3; // Thermistor 电压 int pinR = PB0; // Digital pin #0 Red int pinG = PB1; // Digital pin #1 Green int pinB = PB4; // Digital pin #4 Blue int r; int g; int b; const int nToSleep = 50 ; // 休眠前稳定温度 const int Delay = 100; // 毫秒延迟 double ADCcount; double ADCprevious; int nCount; int ThermistorPin = 1 ; // A1 连 pin #7 (PB2) void setup() { // WDTO_15MS, WDTO_30MS, WDTO_60MS, WDTO_120MS, WDTO_250MS, WDTO_500MS, // WDTO_1S, WDTO_2S, WDTO_4S, WDTO_8S setup_watchdog(WDTO_4S); // 唤醒 sleep_disable(); pinMode(pinT, OUTPUT); digitalWrite(pinT, HIGH); pinMode(pinR, OUTPUT); pinMode(pinG, OUTPUT); pinMode(pinB, OUTPUT); } void loop() { wdt_reset(); // pat K9 ADCcount = analogRead(ThermistorPin) ; if (ADCcount == ADCprevious) ++nCount; if ( nCount > nToSleep ) { // 给低电压 pinMode(pinR, INPUT); digitalWrite(pinR, HIGH); // 上拉电阻 pinMode(pinG, INPUT); digitalWrite(pinG, HIGH); pinMode(pinB, INPUT); digitalWrite(pinB, HIGH); SleepLonger: // 重新睡眠 pinMode(pinT, INPUT); digitalWrite(pinT, HIGH); system_sleep(); sleep_disable(); // 睡眠等待 WDT 触发 pinMode(pinT, OUTPUT); digitalWrite(pinT, HIGH); delay(50); // 缓冲时间 for (uint8_t z=0; z<5; z++) { ADCcount = analogRead(ThermistorPin) ; } if (abs(ADCcount - ADCprevious) < 4) goto SleepLonger; pinMode(pinR, OUTPUT); digitalWrite(pinR, HIGH); pinMode(pinG, OUTPUT); digitalWrite(pinG, HIGH); pinMode(pinB, OUTPUT); digitalWrite(pinB, HIGH); nCount = 0; } else { // 261 = 32F, 447 = 64F, 537 = 75F, 575 = 82F b = map(ADCcount, 261, 447, 100, 255 ); g = map(ADCcount, 435, 574, 250, 100); // 覆盖 green & blue r = map(ADCcount, 575, 1023, 250, 50); if (ADCcount > 574) // HOT: ADCcount 随着温度上升 { // 显示红色如果温度达到设定值 analogWrite(pinR, r); analogWrite(pinG, 255); // 255 = 100% High --> Vcc analogWrite(pinB, 255); // 不显示蓝色 } else { // 温度降低,蓝色想绿色过度 analogWrite(pinR, 255); // 不显示红色 analogWrite(pinG, g); analogWrite(pinB, b); // 低的温度显示深蓝 } } ADCprevious = ADCcount; delay(Delay); } #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) //参考ATtiny85_watchdog_example.zip void system_sleep() { cbi(ADCSRA,ADEN); power_all_disable (); set_sleep_mode(SLEEP_MODE_PWR_DOWN); noInterrupts (); sleep_enable(); interrupts (); sleep_mode(); sleep_disable(); power_all_enable (); sbi(ADCSRA,ADEN); } void setup_watchdog(int ii) { byte bb; int ww; if (ii > 9 ) ii=9; bb=ii & 7; if (ii > 7) bb|= (1<<5); bb|= (1<<WDCE); ww=bb; MCUSR &= ~(1<<WDRF); WDTCR |= (1<<WDCE) | (1<<WDE); WDTCR = bb; WDTCR |= _BV(WDIE); } ISR(WDT_vect) { } [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed