ARDUINO: SERVO AND DC SPINNING MOTORS
1 - Servomotor
- Layout for this exercise:

- The goal of this exercise is to make rotate a servo motor back and forth through the full range of motion, what is 180 degrees.
- A servo motor include feedback circuitry that allows it to be commanded to specific positions, what makes it very useful in robotics.
- The servo has got 3 wires that can be connected with jumper wires: black goes to ground, red goes to 5 V and yellow goes to the Arduino 6 digital pin.
- Code:

- Let's analysis the code.
- First of all the servo library is included, so that all servo functions can be easily accessed. Also, a servo object called "s" is defined:

- Now, the servo object is attached to the Arduino 6 digital pin:

- The movement goes from 0 to 180 degrees:

- The first rotation takes 100 ms delay between each move, stepping 1 degree:

- 1 second of delay between the two rotations:

- The second rotation is faster because there is just 20 milliseconds between each move:

- Testing the circuit:
2 - DC Spinning Motor
- Layout for this exercise:

- The goal of this exercise is to connect a DC motor to an Arduino circuit so that the motor starts spinning and accelerating up to a maximum speed and then decelerates until stopping.
- Two electronic components are used:
a) Transistor: it helps to run the DC motor, because the motor needs more current (even 100 or 250 mA) than provided by the Arduino pins (40 mA). The transistor has 3 pins (Collector: connected to the motor, Base: connected to the resistor, Emitter: connected to GND).
b) Diode: when the motor is spinning and suddenly turned off, the magnetic field inside it collapses, generating a voltage spike that can damage the transistor. To prevent this, a diode is used, diverting the voltage spike away from the transistor. The diode has 2 pins (Cathode: connected to 5 V, Anode: connected to the transistor)
- Code:

- Let's analyse the code.
- The 10 pin is assigned as the motorPin and oputput mode. Also, the serial monitor is initialized with 9600 bauds, inviting the user to enter a speed inside the 0 to 255 interval:



- The analogWrite() function pulses the motorPin using the PWM(Pulse Width Modulation) technique, varying the width of the pulse from 0% to 100%.
- The motor accelerates with 10 milliseconds delay between each move:

- Same thing for the decelerating process:

- Once uploaded the sketch to the board the user enters the rotation speed through the serial monitor interface:

- Testing the circuit: