Tagged: servo, Share project, STM32, ultrasonic sensors
- AuthorPosts
Today’s project will demonstrate how to use the STONE LCD, STM32 microcontroller, ultrasonic sensors, and a servo. The aim of the project is to be able to display the distance measured by the ultrasound in real-time on the STONE LCD.
Consumables used in the project:
2.STM32F411
3. Servo
4. Ultrasonic sensorGUI design:
Start button setting

Stop button setting

Digital display box

Rotating pointer configuration

Compile to see if there are any errors after setting up the display controls

Status output box at the bottom, no errors or warnings

Connect the USB socket on the back of the touch screen, click on download

Download completed

Operation process:
Servo control method:
The servo of the servo is controlled by variable width pulses, the control line is used to transmit the pulses. The parameters of the pulses are minimum, maximum and frequency. In general, the reference signal of the servo has a period of 20 ms and a width of 1.5 ms. The position defined by this reference signal is the middle position. The servo has a maximum rotation angle and the middle position is defined as the exact same amount from this position to the maximum angle as the minimum angle. The most important point is that the maximum angle of rotation may be different for different servos, but the width of the pulse at its intermediate position is a certain amount, which is 1.5ms.The angle is generated by a continuous pulse from the control line. This method of control is called pulse modulation. The length of the pulse determines how much the servo will turn. For example, a 1.5 ms pulse will get to the middle of the rotation (for a 180° servo, this is the 90° position). When the control system issues a command to move the servo to a certain position and to keep it at that angle, the external forces will not cause the angle to change, but this is subject to an upper limit, which is the maximum torque. The angle of the rudder will not remain constant unless the control system keeps sending out pulses to stabilise the angle of the rudder.
When the rudder receives a pulse of less than 1.5ms, the output shaft will rotate counterclockwise by a certain angle, using the middle position as the standard. The opposite is true for pulses received greater than 1.5ms. Different brands, or even different servos of the same brand, will have different maximum and minimum values. Generally speaking, the minimum pulse is 1ms and the maximum pulse is 2ms.To summarise.
Firstly, the leads of the servos are generally controlled by three wires (no contact has been made with those that are not three wires), red for power, brown for ground and yellow for signal. When controlling the servo, you need to keep giving PWM waves in order to make the servo have torque at a certain angle.STM32 core board.
The microcontroller program is programmed using the firmware library STM32CUDEMX:.
Pinout diagram.
Timing configuration.

Hardware connections.

Ultrasonic and servo connections schematic.

J17 jumper on the back of the STONE screen disconnected, selected to TLL level and microcontroller communication:

Importing the GUI generated file into the touch screen
Source code analysis:
void MX_USART1_UART_Init(void)
{huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
Mode = UART_MODE_TX_RX. huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) ! = HAL_OK)
{
Error_Handler();
}}
The servo driver is driven using the microcontroller’s own PWM generator, configured as follows

void MX_TIM2_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};htim2.Instance = TIM2;
htim2.Init.Prescaler = 1000-1;
CounterMode = TIM_COUNTERMODE_UP. htim2.Init;
htim2.Init.Period = 1000-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim2) ! = HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) ! = HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 51;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) ! = HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim2);}
Sensor ranging uses interrupt control and is configured as follows.
Ranging procedure:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_1)
{
if( (HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin) == 1) && (bit_bit == 1) )
{
__HAL_TIM_SetCounter(&htim3,0);
HAL_TIM_Base_Start_IT(&htim3);
}
else if( (HAL_GPIO_ReadPin(ECHO_GPIO_Port,ECHO_Pin) == 0) && (bit_bit == 1) )
{
HAL_TIM_Base_Stop_IT(&htim3);
cm = msHcCount*1000;
cm += __HAL_TIM_GetCounter(&htim3); __HAL_TIM_SetCounter(&htim3,0); msHcCount = 0;
bit_bit = 0;
}}}
The microcontroller packages the measured data and sends it to the touchscreen: the
void HOME(void *argument)
{
uint8_t data_ico[24] = {0xA5, 0x5A, 0x14, 0x85, 0x00, 0x00, 0x00 ,0x00 ,0x00 ,0x00 ,0xff ,0xff ,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff};
uint8_t k = 0;
uint16_t j = 0,bit = 0;for(;;)
{if(bit_state)
{
if(bit == 0)
{
j++;
}
else
{
j–;
}if(bit == 0 && j >= 180)
{
bit = 1;
}
else if(bit == 1 && j <= 0)
{
bit = 0;
}
}
jiaodu = j;
for(k=0;k<7;k++) { if(Y>=600)
Y = 600;
if(Y<170) Y = 170; data_ico[5] = X>>8;//x
data_ico[6] = X;
data_ico[7] = Y>>8;//y
data_ico[8] = (Y+k)&0x00ff;
UART1_Send_Array(data_ico,23);
}
data_send[4]=0x06;
data_send[5]=0xc8;
data_send[6]=juli>>8;
data_send[7]=juli;
UART1_Send_Array(data_send,8);data_send[4]=0x00;
data_send[5]=0x01;
data_send[6]=0x00;
data_send[7]=jiaodu;
UART1_Send_Array(data_send,8);vTaskDelay(pdMS_TO_TICKS(10));
osDelay(1);
}
}
- AuthorPosts
- You must be logged in to reply to this topic.