Control Stepper motor using Rotary Angle Sensor
I have covered a tutorial Stepper Motor a while back, and today we will see how can we control the positioning of stepper motor using a Rotary angle sensor. If you don’t have this sensor, you can use any potentiometer also.
The rotary angle sensor is a type of potentiometer, which produces analog output between 0 and Vcc (3.3V) . The angular range is 300 degrees with a linear change in value. The sensor works with ADC
As i have mentioned above that the sensor works with ADC, so we need to enable the ADC
- As you can see above, I have enabled channel 1 of ADC1
- Resolution is selected as 12 bits
- Continuous Conversion is enabled
- DMA request is also enabled, as we will be using DMA in this tutorial
- DMA mode is circular, and the Data width is Half Word (16 bits)
This is all for ADC setup, now we will setup the 4 Pins as output, to control our stepper motor
As shown above, I am using Pins PA4, PB0, PC1 and PC0 as the output pins. These Pins will be connected to the stepper Motor IN1, IN2, IN3 and IN4 respectively
We also need to configure a timer to give delays in microseconds. This will be used to control the RPM of the stepper
Here I am using Timer6 for this purpose. I don’t think I need to explain this part, but anyway you can check the explanation Microsecond/Nanosecond delay in STM32
This completes the setup part. The connection between the stepper, Sensor and the microcontroller is shown below
Please go through the basic concepts of stepper motor. I will not cover them here as I have already covered them -> Interface Stepper motor with STM32
First of all we will do some basic defining.
- Here First of all we will define the respective pins, that are connected to the Stepper motor
- After defining pins, we need to define the number of steps per revolution. I will be using the wave Drive in this tutorial, so the number of steps are 2048. If you want to use half drive, these number of steps should be 4096
- Then create a function that can produce delay in microseconds
- next create functions to Set and Reset the Pins
- Stepper_wave_drive will drive the motor for the required number of steps. In wave Drive, motor takes 2048 steps to complete 1 revolution, so we have to call it many times depending on the angle of rotation.
- then we have a function to set the RPM for the motor. This basically works like a delay between 2 stpes.
- stepper_step_angle will make the motor step by the given angle. You can also control the Direction of rotation (1->CCK, 0->CK), and the RPM of the motor
This is a relatively new function and isn’t present in the stepper motor tutorial.
- Stepper_rotate is the function to rotate the motor
- here we will first calculate the change in angle.
- If this change is positive, the motor will rotate in the CK direction by the “change” amount
- if the change is negative, the motor will rotate in the CCK direction
- If the change is less than 1, the motor will not rotate. This is to compensate the fluctuation in the ADC reading
- This is a callback function and will be called when the ADC will finish the conversion
- Here we will first calculate the voltage using the ADC value. This voltage will correspond to the angle sensor has been rotated.
- using this voltage, we will calculate the angle
- and finally feed the angle to the motor
In the main function, we will just start the timer, and ADC with the DMA
- STM32 Ethernet #12 HTTP Server using AJAX PART2 June 28, 2022
- STM32 Ethernet #11 HTTP Server using AJAX PART1 June 24, 2022
- How to use UART in ESP32 || ESP-IDF June 12, 2022
- Getting started with ESP32 and ESP-IDF || LED Blinking June 5, 2022
- Custom characters in LCD 1602 || STM32 May 25, 2022
- STM32 Ethernet #10 HTTP Server (Basic) using LWIP NETCONN (RTOS) May 18, 2022