Xh711 tension sensor data pin issue

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Xh711 tension sensor data pin issue

Postby kyrpav » Wed Jan 19, 2022 8:12 pm

Hi, i am trying to make a simple program to read tension from a load cell using the xh711 24bit adc. chip uses 2 pins. one for scl (esp32 output) and another dout for data (esp32 input).
In order the chip to be powered on scl has to be low and then when the conversion is ready the dout goes from high to low.
After this you have to send pulses to scl in order to shift the data out from the dout pin.

My problem is that when i connect everything dout=18 and scl=19 then the dout is always on for some reason the esp32 it keeps it on so i can not ready anything. If i manually disconnect the pin and reconnect it then it is working and data are shifted out. It is possible that then the pin never goes up.
  1. Xh711::Xh711(gpio_num_t dout,gpio_num_t scl,Gain gainValue, bool smoothed, uint8_t sampling){
  2.     this->dout=dout;
  3.     this->scl=scl;
  4.     this->gainValue=gainValue;
  5.     this->smoothed = smoothed;
  6.     this->sampling = sampling;
  7.     this->offset = 0 ;
  8.     this->conversionFactor = 1;
  9.     this->value = -1 ;
  10.     printf("before init\n");
  11.     this->init(gainValue);
  12. }
  13.  
  14. Xh711::~Xh711(){
  15. }
  16.  
  17. void Xh711::init(Gain setGain){
  18.     gpio_pad_select_gpio(this->dout);
  19.     gpio_pad_select_gpio(this->scl);
  20.  
  21.     gpio_set_direction(this->dout,GPIO_MODE_INPUT);
  22.     gpio_set_direction(this->scl,GPIO_MODE_OUTPUT);
  23.     //even used the pulldown resistor but nothing changed
  24.     //gpio_pulldown_en(this->dout);
  25.    // gpio_pullup_dis(this->dout);
  26.     this->powerOff(true);
  27.     this->powerOff(false);
  28.     printf("in init before get gain\n");
  29.     vTaskDelay(10000/portTICK_PERIOD_MS);
  30.     this->updateGainNextMeasures(setGain);
  31.     // get values for calibrationMass,
  32.     // conversionFactor , offset from ram or calibrate(first=true)
  33.     this->calibrate(true);
  34. }
  35.  
  36. void Xh711::powerOff(bool flag){
  37.  
  38.     if(true){
  39.         gpio_set_level(this->scl,0);
  40.         gpio_set_level(this->scl,1);
  41.     }else{
  42.         gpio_set_level(this->scl,0);
  43.     }
  44.    
  45.     vTaskDelay(200/portTICK_PERIOD_MS);
  46. }
  47.  
  48. bool Xh711::isReady(){
  49.    
  50.     if (!gpio_get_level(this->dout)){
  51.         ets_delay_us(1);
  52.         return true;
  53.     }
  54.     return false;
  55. }
  56.  
  57. void Xh711::sendPulse(uint8_t times, int8_t delayUs){
  58.    
  59.     for (int i = 0 ; i< times; i++){
  60.         gpio_set_level(this->scl,1);
  61.         ets_delay_us(delayUs);
  62.         gpio_set_level(this->scl,0);
  63.         ets_delay_us(delayUs);
  64.     }
  65. }
  66.  
  67.  
  68. void Xh711::getRawData(){
  69.     int8_t pulses=24;
  70.     this->rawData = 0 ;
  71.     for (int i = 0 ; i < pulses ;i++){
  72.         sendPulse(1,3);
  73.         this->rawData |= gpio_get_level(this->dout) << (23 - i);
  74.     }
  75.     sendPulse(this->gainValue,3);
  76. }
  77.  
  78. void Xh711::updateGainNextMeasures(Gain gainToSet){
  79.     this->gainValue = gainToSet;
  80.     this->calculateRawData();
  81. }
  82.  
  83. void Xh711::calculateRawData(){
  84.    
  85.     // printf("wait to be ready\n");
  86.     while (!this->isReady()){
  87.         pdMS_TO_TICKS(100);
  88.     }
  89.     //printf("ready");
  90.     if(this->smoothed){
  91.         uint32_t sum=0;
  92.  
  93.         for(uint8_t i=0;i< this->sampling ;i++){
  94.             this->getRawData();
  95.             sum += this->rawData;
  96.         }
  97.  
  98.         this->rawData = sum / this->sampling ;
  99.     }else{
  100.         this->getRawData();
  101.     }
  102.  
  103.    
  104.     this->rawData =  this->rawData ^ 0x800000;
  105.     this->rawData |= 0xff000000;
  106.     if (this->rawData > 0xFFFFFF){
  107.         ESP_LOGW(TAG,"Data out of limit");
  108.     }
  109.    
  110. }
  111.  

A second issue is that since the dout is always on then from the isReady() function i get error from the watchdog also but this will be solved if the pin dout works properly.

Screenshot 2022-01-19 at 8.15.20 AM.png
Screenshot 2022-01-19 at 8.15.20 AM.png (45.18 KiB) Viewed 3303 times
Screenshot 2022-01-19 at 8.13.43 AM.png
Screenshot 2022-01-19 at 8.13.43 AM.png (77.9 KiB) Viewed 3303 times

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Re: Xh711 tension sensor data pin issue

Postby kyrpav » Wed Jan 26, 2022 10:21 pm

Sorry the post. issue was really on poweroff. if true statetment keeps everytime the adc in poweroff condition

Who is online

Users browsing this forum: No registered users and 52 guests