MagicCode Lesson 10
Use servo motor with magiccode
A servomotor is an electrical device, which can push or rotate an object with great precision. If you want to rotate an object at some specific angles or distance, then you use a servo motor. It is just made up of a simple motor, which runs through servomechanism.
- Using servo motor with Magicbit
The servo motor works on the PWM (Pulse Width Modulation) principle, which means its angle of rotation, is controlled by the duration of the pulse applied to its control PIN. A servomotor is made up of a DC motor, which is controlled by a variable resistor (potentiometer), and some gears. Servomotors control position and speed very precisely. Now a potentiometer can sense the mechanical position of the shaft. Hence, it couples with the motor shaft through gears. The current position of the shaft is converted into an electrical signal by a potentiometer and is compared with the command input signal. In modern servomotors, electronic encoders or sensors sense the position of the shaft. A pulse of 1ms will move the shaft anticlockwise at -90 degrees, a pulse of 1.5ms will move the shaft to the neutral position that is 0 degrees and a pulse of 2ms will move the shaft clockwise at +90 degrees.
To implement this project, first, connect the magic servo motor to the Magicbit. Then Drag and Drop the following blocks from the block palette into the scripting area to create the following Script.
This program rotates the servo motor by 180 degrees, with 45 degrees step at a time. The program starts by clicking the green flag on the MagicCode interface. The servo should be connected to a PWM pin and, in this example, the servo has connected to Magicbit Pin 32. According to the script, at first, the servo rotates 45 degrees and stays one second at that position. In the same way, it rotates up to 180 degrees and stops.