Easy Embedded Programming: Spin the Motor

Published By

The final video wraps up the series and shows off your accomplishments. In the end, the speed of the motor will be controlled by the distance between your hand and the proximity sensor.

In the previous video, we learned how to drive a pin at various frequencies with or without a PWM, how to generate interrupt to perform an action every x microseconds thanks to an interrupt and a timer, and how to communicate with external components by programming an I2C and an SPI bus. Combining all this knowledge, we can now create a project where the speed of the motor will depend on the distance between our hand and the proximity sensor. Again, for the microcontroller we use the PSOC4200 BLE from Cypress, the proximity sensor is the VL6180 from STMicroelectronics and the stepper motor driver is the L6474 from STMicroelectronics. The device with the I2C was a proximity sensor and the device with the SPI bus was a stepper motor driver. 

See related product

VL6180XV0NR/1

STMicroelectronics Specialized Sensors View

See related product

L6474HTR

STMicroelectronics Motor Controller and Driver ICs View
 
The project has the following blocks: a 32 kilo Hertz clock connected to a timer with a period of eight thousand counts. This timer triggers an interrupt every 250 ms where we will check the value of the proximity sensor. 
 
The 32kHz clock feeds a PWM to generate a square signal. A period of 320 counts combined with the 32kHz clock drives the motor at a speed of 100 step/s. In order to have a 50% duty cycle, the compare value of half of the period value. To change the speed of the motor, we will overwrite these values in the program. The output line of the PWM is connected to the STEP pin of the L6474 and each rising edge on this pin moves the motor of one step.
 
The I2C block allows communicating with the proximity sensor VL6180 and the SPI bus communicates to the stepper motor driver L6474. The I2C is programmed as a master at 100kHz.
 
The I2C SCB component uses an internal interrupt that has a priority level of 3 by default. As in the program it is called while we are in the interrupt service routine triggered by the timer that has also a default priority value of 3, it is never called and there is no I2C communication. If we change the interrupt level of the I2C at 2, it is then called and the I2C communication is then performed correctly.
 
Arduino pinout constraints generate constraints to use the I2C and the SPI in SCB mode. Indeed it would need to be in the same SCB component. To overcome this issue, the SPI master in the UDB is used instead of the SPI of the SCB. APIs are very similar. In the UDB mode, pin connections are not automatic, an pin components must be added in the schematic.
The polarity of the SPI bus is programmed with CPHA = 1 and CPOL = 1, MSB first and data bits equal to 8. The MISO pin is the input pin and the MISO, CLOCK and Chip Select pins are configured as outputs in strong drive mode for all of them.  

two more pins are necessary : a reset pin and a direction pin.
Regarding the program, the SPI, I2C, Timer and PWM blocks must be started. We also calls methods programmed in the L6474.c file to reset and configure the L6474. The timer is programmed with an interrupt: in the interrupt, we start to read a value from the proximity sensor. Based on the distance, a new speed is calculated and the PWM period and compare values are overwritten. The feedback from the proximity sensor is between 0 (object close to sensor) and 255 (no object detected). The closer the object is, the faster the motor spin.
 
In the L6474.c file, we have a method to reset the motor driver by changing the level of the reset pin with some delays, a 2nd method to send an SPI command 
 
and a third one to configure the driver and call the method sending the SPI packets. 
 
The code was not optimized in order to make it very easy to read. Please refer to the L6474 reference manual to understand the configuration parameters.
Finally, in the VL6180.c file, we program the method for the proximity sensor.  Before we can use the proximity sensor, we need to configure it and confirm the device is ready. Please read the VL6180X specifications for more details.
 
 
We just follow the recommendations of an application note about the VL6180. We perform a range measurement in single shot mode. So in our program, the following methods:

- to start the measurement, 
- wait for the measurement to be ready and finally a method to read the value.
- Read the value of the proximity sensor
 
The final step is to build and program the board. The motor spins at low speed (100 step/s) and when a project get close to the proximity sensor, the motor can spin up to 1,000 step/s the distance, depending on the distance, the speed will vary between these two values.

Latest Videos

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here