cancel
Showing results for 
Search instead for 
Did you mean: 

I want to run the load cell sensor but I couldn't,I can't get data.

nur1
Associate

I want to run the load cell sensor but I couldn't, I use my codes stm32f407 and HX711 like this, can you help me with this? I couldn't do the calibration because I can't get data.

 

include "main.h"

#include "stm32f4xx_hal.h"




extern TIM_HandleTypeDef htim2;




/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */




/* USER CODE END Includes */




/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */




/* USER CODE END PTD */




/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */




/* USER CODE END PD */




/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */




/* USER CODE END PM */




/* Private variables ---------------------------------------------------------*/




/* USER CODE BEGIN PV */




/* USER CODE END PV */




/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

/* USER CODE BEGIN PFP */




/* USER CODE END PFP */




/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */




#define DT_PIN GPIO_PIN_8

#define DT_PORT GPIOB

#define SCK_PIN GPIO_PIN_9

#define SCK_PORT GPIOB




uint32_t dara = O;

float originalweight = 1;  // in milli gram

float HX711weight = 1;

int weight;




void microDelay(uint16_t delay)

{

  __HAL_TIM_SET_COUNTER(&htim2, 0);

  while (__HAL_TIM_GET_COUNTER(&htim2) < delay);

}







int32_t getHX711(void)

{

  uint32_t data = 0;

  uint32_t startTime = HAL_GetTick();




  for(int8_t len=0; len<24 ; len++)

  {

    HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_SET);

    microDelay(1);

    data = data << 1;

    HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_RESET);

    microDelay(1);

    if(HAL_GPIO_ReadPin(DT_PORT, DT_PIN) == GPIO_PIN_SET)

      data ++;

  }

  data = data ^ 0x800000;

  HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_SET);

  microDelay(1);

  HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_RESET);

  microDelay(1);

  return data;

}




int weigh()

{

  int32_t  total = 0;

  int32_t  samples = 100;

  int milligram;

  float coefficient;

  for(uint16_t i=0 ; i<samples ; i++)

  {

      total += getHX711();

  }

  int32_t average = (int32_t)(total / samples);

  coefficient = originalweight / HX711weight;

  milligram = (int)(average-dara)*coefficient;

  return milligram;

}




/* USER CODE END 0 */




/**

  * @brief  The application entry point.

  * @retval int

  */

int main(void)

{

  /* USER CODE BEGIN 1 */




  /* USER CODE END 1 */




  /* MCU Configuration--------------------------------------------------------*/




  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();




  /* USER CODE BEGIN Init */

  __HAL_RCC_TIM2_CLK_ENABLE();

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 0;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK) {

 Error_Handler();

 }

  /* USER CODE END Init */




  /* Configure the system clock */

  SystemClock_Config();




  /* USER CODE BEGIN SysInit */




  /* USER CODE END SysInit */




  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  /* USER CODE BEGIN 2 */




  HAL_TIM_Base_Start(&htim2);

  HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_SET);

  HAL_Delay(10);

  HAL_GPIO_WritePin(SCK_PORT, SCK_PIN, GPIO_PIN_RESET);

  HAL_Delay(10);

  /* USER CODE END 2 */




  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

 weight = weigh();

 HAL_Delay(200);

 // in milligram

    /* USER CODE END WHILE */




    /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

 

3 REPLIES 3

Have you used an oscilloscope to verify that your PD_SCK & DOUT lines are actually working correctly?

https://www.digikey.co.uk/htmldatasheets/production/1836471/0/0/1/hx711.html 

 

Do you have an Arduino to hand to check against?

https://learn.sparkfun.com/tutorials/load-cell-amplifier-hx711-breakout-hookup-guide/all

 

I don't have an oscilloscope. could I have made a mistake in code writing?

Techn
Senior III

As per data sheet you need to chek if data is ready , then do the read. Pl set the other pins of hx711 as per datasheet. Timing is very critical. 

If you feel a post has answered your question, please click "Accept as Solution".