Project: Rotary Encoder & Servo

Can we control a servo with a rotary encoder? Can we make it turn in the same direction as we turn the rotary encoder? What could we attach to the servo arm? Perhaps use this to control a crane or fork lift? Can it be attached to some lego to make something interesting?

Step 1: Set up the rotary encoder. Review Micro:bit Rotary Encoder and connect up the hardware. Run the test code to make sure the encoder is working.

Step 2: Can you turn this code into a function? Sit in the loop and call a function checkEncoder(), which will report back the direction (if any) it is being turned. Perhaps return 0 for stationary, 1 for turning clockwise and 2 for turning anti-clockwise. Consider returning two values to also report the state of the button.

Step 4: Connect the servo. Follow Micro:bit SG09 Servo and test the servo. Note, both examples use pin0 so you will have to make adjustments.

Step 5: Can you turn the servo in the same direction as the rotary encoder? You may need to write a function to tell the servo to move by X number of degrees. Try playing with different values for X and different sensitivities to control how fast the encoder turns the servo. Don’t forget to make sure you do not try to turn the servo beyond it’s limits.

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.