HW14 - ndm736/ME433_2023 GitHub Wiki

Setup PWM for brushed dc motor control and RV servo control.

We will use the dual motor controller on a breakout board from Pololu to drive the two brushed DC TT gearmotors and wheels.

The DRV8835 can be supplied with 2-11V on the VIN pin, and can supply as mush as 1.5A per motor for a few seconds. There is reverse bias protection between the VIN and VM pin, which incurs a slight voltage penalty, but worth it to prevent burnouts if the battery is plugged in backwards. Too bad about the max of 11V, a common 3 cell LIPO battery can be as high as 12.6V.

When the mode pin is HIGH, the PHASE pin is interpreted as direction and the ENABLE pin is interpreted as speed, so a regular IO pin can be applied to PHASE and a PWM pin to ENABLE. If mode is LOW, both pins need to be PWM, which is more flexible if you want to drive a stepper motor, but with regular brushed DC motors DIR and SPEED is more convenient. PWM is typically 20kHz, to stay above the audible range. The motors typically have large amount of friction, so a minimum duty cycle, sometimes as much as 50%, might be required to start the motor rotating. The static friction is greater than the kinetic friction, so once the motor starts, it can usually keep going at a smaller duty cycle.

An RC servo motor has a built in driver and PID controller. The connections are ground to BROWN, signal to ORANGE, and power (4.5 to 6V) to RED. Use the extra long header pins to connect the RC plug to the breadboard.

The signal to an RC servo is also PWM, but at a specific frequency and limited duty cycles. The frequency is 50Hz, or 20ms, and a duty cycle of 1.5ms will command the servo to 0 degrees and a duty cycle of 2.5ms will command the servo to 180 degrees (each manufacturer is different, 1ms to 2ms is a safe place to start, note the angles, and expand from there).

The Pico W will be busy doing other things, so use the PIC to control the motors. The PIC requires a timer to set the frequency of the PWM, and an output compare (OC) module for a pin. You will need two timers (the only options are timer2 and timer3) and three OC pins (there are OC1-5 available).

The PIC timers are 16bit, so they can count to a maximum of 65535. They can count every SYSCLK, or every other, or every fourth, and so on, using a prescaler. Set the timer prescaler so that the timer will count as high as possible for the appropriate amount of time without being larger than 65535, to maximize the resolution of the PWM duty cycles.

Example code:

T2CONbits.TCKPS = 2;     // Timer2 prescaler N=4 (1:4)
PR2 = 1999;              // period = (PR2+1) * N * (1/48000000) = 6 kHz
TMR2 = 0;                // initial TMR2 count is 0
OC1CONbits.OCM = 0b110;  // PWM mode without fault pin; other OC1CON bits are defaults
OC1CONbits.OCTSEL = 0;   // Use timer2
OC1RS = 500;             // duty cycle = OC1RS/(PR2+1) = 25%
OC1R = 500;              // initialize before turning OC1 on; afterward it is read-only
T2CONbits.ON = 1;        // turn on Timer2
OC1CONbits.ON = 1;       // turn on OC1

Don't forget to assign the OC pin using the reprogrammable IO feature!

For this assignment, use the PIC to sweep the angle of the RC servo from 45 degrees to 135 degrees every 4 seconds. Upload your code to a folder names HW14 in your repo and make a video of the RC servo for Canvas.

What can you do with motors and servos?

Some of Nick's favorite channels:

An endless number of mechanisms on youtube

How it's made on youtube

All the cool mechanisms