newlandmark

[Tuya Smart] IoT temperature and humidity sensor

 
Overview

Lichuang & Tuya Training Camp Phase 2--Internet of Things Temperature and Humidity Sensor

The small temperature and humidity sensor based on Tuya Smart automatically collects the current temperature and humidity data, uploads it to the cloud through the STM32 & wifi module, and sends it to the APP for display.

main content

  • Overall introduction
  • Part of the circuit
  • Partial procedure
  • Precautions
  • actual effect

Overall introduction

  • This project is a temperature and humidity sensor based on Tuya Smart, using a low-code MCU solution. SHT20 is responsible for collecting temperature and humidity. The Tuya IoT module is used in combination with the STM32F0 microcontroller to collect and report temperature and humidity data, and visualize the data.
  • The sensor can be composed of two PCBs, connected through pin headers and four fixed posts. The overall size is: 56mm*44mm*15mm. The lower board contains various circuits required by the sensor, and the upper layer is an OLED module.
  • For power supply, you can use 3.7V lithium battery or Type-C interface for power supply. When both are connected at the same time, USB power supply is used by default. The button in the lower right corner is used to realize the one-button power on and off function.
  • The OLED interface is an IIC communication interface, which can display the current temperature, humidity and other contents on the OLED. The OLED can also be controlled whether to turn on the display through the upper right button.

Part of the circuit

  1. power supply :

    • The first is the input source of the power supply: USB interface or single-cell lithium-ion battery (3.7V input). Both power supplies are the most common power sources. In order to better manage the two, lithium batteries and USB are used at the input end of the power supply. Automatic switching circuit, which consists of a PMOS tube, Schottky diode and resistor, can do:

      When USB and battery are connected at the same time, battery power supply will be cut off.

      When USB is disconnected, switch back to battery power

      The schematic connection is as follows

      First of all, what needs to be paid attention to is the direction of the PMOS tube. The inflow direction of the battery power is indeed from D (drain) → S (source), which utilizes the body diode of the PMOS.

      • When only USB supplies power, PMOS is turned off, and VBUS is supplied to subsequent circuits after passing through the Schottky diode. Since the voltage drop of the Schottky diode is low, the actual measurement is about 0.26V, so the voltage obtained by VCC is about 4.8V.
      • When the lithium battery supplies power alone, R4 pulls the G (gate) voltage down to 0V, PMOS is turned on, and VBAT flows through the MOS and is supplied to the rear circuit. Since the internal resistance of the MOS is very small, there is only a small voltage loss. The actual measurement is only 0.005V, so VCC ≈ VBAT.
      • When both are connected at the same time, VBUS flows through D3, VCC is around 4.8V, and the gate voltage of PMOS is VBUS = 5V, PMOS is turned off, and VCC (4.8V) > VBAT, so the battery is cut off.
      • If the PMOS direction is rotated and VBAT changes from S → D, then when both are connected at the same time, USB current will flow into the lithium battery, causing problems.
      • When both are connected at the same time, if the USB is suddenly unplugged, since the capacitor in the subsequent circuit will store a certain amount of charge, there will not be a situation where the whole power is turned off first and then turned on, but the effect of direct switching without losing power in the middle.
    • One-key on/off circuit : With the help of the on/off circuit, you can realize one-key on/off by pressing the button on the lower right. Press and hold for two seconds to turn on the phone, and then press it again to turn off. The schematic is as follows

      • When not in use, the PMOS is turned off and no power flows into the
      • When the button is pressed, the PMOS gate is pulled down to ground through the button, the PMOS is turned on, power flows in, and the entire circuit is powered on. At this time, the PWR_ON pin voltage needs to be raised in the initialization program, the transistor is turned on, and the MOS gate is pulled Low, so that the circuit will always be on even if the button is released.
      • If the shutdown function is required, set the PWR_DOWN pin to input detection. When the button is pressed again, the PWR_DOWN pin detects a low level, and the program controls the PWR_ON pin to output a low level, the transistor is cut off, and the PMOS is turned off again to achieve Shutdown function.

    • LDO step-down circuit : After the power flows in, it is stepped down to 3.3V through an LDO circuit and then supplied to the entire system. In order to protect the circuit, a PTC and two TVS diodes are added. The overall current peak demand is about 200mA, and the average normal operating current 100mA.
  2. WiFi module: Use Tuya Smart's WB3S module to communicate with STM32 through the serial port. Pull pin 1 high, solder R6 and leave R2 floating. In order to meet the instantaneous high current demand, you can add an extra capacitor at the VDD end.
  3. STM32 system: The microcontroller model used this time is STM32F030F4P6, M0 core, TSSOP-20 package, small size. It is responsible for processing the data of the SHT20 sensor, controlling the OLED display, communicating with WB3S through the serial port, etc., including reset, crystal oscillator, SWD interface, etc. circuit and fully utilize all its pins.
  4. SHT20: Temperature and humidity sensor, IIC communication, so two pull-up resistors are necessary. In order to reduce interference from the PCB itself, grooves are dug around the chip.
  5. OLED module: The module circuit is relatively simple, including an LDO circuit and necessary resistor-capacitor components. It uses IIC and STM32 to communicate. The power supply voltage can be 5V or 3.3V.
  6. Buttons: A total of 3 side-mounted buttons are included, which take up less space and are easy to press. The lower SW3 controls the overall switch, the upper SW1 is the network distribution button, and SW2 controls the OLED display. It is turned on by default. Press it again to turn off the display.
    • A capacitor is connected in parallel to the button to achieve simple hardware debouncing.

      program part

  • For official documentation tutorials, please see Documentation Tutorials
  • For the official video tutorial, please see MCU SDK transplantation
  • The program idea this time is relatively clear. After creating the F0 project, follow the tutorial to transplant the MCU SDK downloaded from Tuya into the project, and then add the SHT20 driver, OLED driver, button program and program to control the power on and off.
  • After STM32 initializes each module, turn it on, enter while(1), and use the button to trigger the network configuration. After the network configuration is successful, use SHT20 to collect data and report the collected data. The core program is
// SDK函数
wifi_uart_service();
// 温湿度采集
temp=SHT2x_GetTempPoll();//获取SHT20 温度
Humi=SHT2x_GetHumiPoll();//获取SHT20 湿度
// 温湿度上报
mcu_dp_value_update(1,(long)temp*10); //温度数据上报,采集的 float 数据转换为 long;
mcu_dp_value_update(2,(long)Humi); //湿度数据上报,float 转换为 long ;
// 温湿度OLED显示
OLED_WS_DIS(temp,Humi);
  • The complete program has been placed in the attachment. Please correct me.

Precautions

  • The one in my hand is a micro usb interface, but the current picture has been changed to a Type-C interface.
  • The overall average current of the board is 100mA and the peak current is 200mA, so when choosing an LDO, you need to choose a model with a maximum output of more than 300mA.
  • When using lithium batteries for power supply, since the board itself does not have a battery management chip, you must choose a lithium battery with a discharge protection board. The battery should not be used for a long time. When the voltage is insufficient, it should be charged in time.
  • Regarding the OLED display module, you can use common ones. The module used here is the module from Jixin. I originally planned to make an OLED module with the same size as the lower panel and use 1.3-inch OLED, but due to various reasons, I gave up using it.
  • In terms of the program, there are several things to pay attention to. For example, if you use a timer, you should pay attention to the interrupt priority of the serial port and timer. When transplanting the SDK, the serial port sending function uses a single-byte function, etc. You need to pay attention.
void USART1_SendByte(unsigned char data)//单字节发送函数
{   
    while((USART1->ISR & USART_FLAG_TXE) != USART_FLAG_TXE);
    USART1->TDR = data;
}

actual effect

  • A few pictures:

    If you have any questions, please leave a message and comment.

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-21 09:13:12

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号