[转载] [Arduino模块]TPA81 8Pixel热传感器

2013-12-181.1万
AI 快速预览详细 收起
TPA81 8Pixel热传感器是一种通过I2C接口与Arduino连接的温度传感器,能够提供8个像素的温度数据。配合LCD03显示屏,可以实时显示温度信息。该传感器适用于环境监控和智能家居系统。代码示例展示了如何通过I2C读取温度数据并显示在LCD03上。
[Arduino模块]
TPA81 8Pixel热传感器

电路示意:



代码示例:
  1. /********************************************
  2. *    arduino example for TPA81 and LCD03    *
  3. *         TPA81 controlled by I2C           *
  4. *        LCD03 controlled by serial         *
  5. *                                           *
  6. *         By James Henderson 2012           *
  7. ********************************************/
  8. #include <Wire.h>
  9. #include <SoftwareSerial.h>
  10. #define ADDRESS             0x68                                   // Address of TPA81
  11. #define SOFTREG             0x00                                   // Byte for software version
  12. #define AMBIANT             0x01                                   // Byte for ambiant temperature
  13. #define LCD_RX              0x02                                   // Pin for rx
  14. #define LCD_TX              0x03                                   // Pin for tx
  15. #define LCD03_HIDE_CUR      0x04
  16. #define LCD03_CLEAR         0x0C
  17. #define LCD03_SET_CUR       0x02
  18. #define LCD03_RETURN        0x0D
  19. SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX);  // Defines serial port for LCD03
  20. int temperature[] = {0,0,0,0,0,0,0,0};                   // Array to hold temperature data
  21. void setup(){
  22.   lcd_03.begin(9600);                                    // Starts software serial port for LCD03
  23.   Wire.begin();
  24.   delay(100);                                            // Wait to make sure everything is powerd up
  25.   
  26.   lcd_03.write(LCD03_CLEAR);   
  27.   lcd_03.write(LCD03_HIDE_CUR);
  28.   byte software = getData(SOFTREG);                       // Get software version
  29.   lcd_03.print("TPA81 Example  V:");
  30.   lcd_03.print(software);                                // Print software version to the screen
  31. }
  32.   
  33. void loop(){
  34.   for(int i = 0; i < 8; i++){                            // Loops and stores temperature data in array
  35.   temperature[i] = getData(i+2);
  36.   }
  37.   
  38.   lcd_03.write(LCD03_SET_CUR);                           
  39.   lcd_03.write(21);                                      // Moves cursor to space 21
  40.   for(int x = 0; x < 8; x++){                            // Loop prints each member of temperature to LCD03 followed by a space
  41.     if(x==4)                                             // If x is 4 perform a carriage return to format the results on the LCD03 for easier reading
  42.       lcd_03.write(LCD03_RETURN);
  43.     lcd_03.print(temperature[x]);
  44.     lcd_03.print(" ");
  45.     delay(50);                                           // Wait befor printing next value to give time for everything to be sent
  46.   }
  47.   
  48.   lcd_03.write(LCD03_RETURN);                          
  49.   int ambiantTemp = getData(AMBIANT);                    // Get reading of ambiant temperature and print to LCD03 screen
  50.   lcd_03.print("Ambiant: ");
  51.   lcd_03.print(ambiantTemp);
  52. }
  53. byte getData(byte reg){                                   // Function to receive one byte of data from TPA81
  54.   Wire.beginTransmission(ADDRESS);                        // Begin communication with TPA81
  55.     Wire.write(reg);                                      // Send reg to TPA81
  56.   Wire.endTransmission();
  57.   Wire.requestFrom(ADDRESS, 1);                           // Request 1 byte
  58.   while(Wire.available() < 1);                            // Wait for byte to arrive
  59.   byte data = Wire.read();                                // Get byte
  60.   return(data);                                           // return byte
  61. }
复制代码





创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -