Learn 6 ways of driving a servo motor using Arduino. You will get a connection diagram, the Arduino code as well as an online Arduino simulation. You will use the free Arduino simulator from Wokwi to run all the examples given here. You will be able to run the code, modify it and see the results instantly online! No hardware is needed!

The Servo motor is driven using a PWM signal. The PWM width will give the indication of the required angle of rotation. Even if the servo motor slightly offsets from the required setting, in the next cycle of PWM, it would correct itself automatically. The Servo motor holds a comparator inside which will compare the incoming width of the pulse and drive the servo motor to the appropriate angle.

Servo motors find their applications in a variety of fields. It is used to rotate cameras on a robotic car, make walking tripods, or hexapods, robotics arms, cars that will paint and create art! and many more.

1.  Basic Arduino and Servo motor example

Project Link:

https://wokwi.com/arduino/projects/300397472417579530

Code and the connection diagram are always available in the link above

https://imgur.com/pA2O31Zservo motor example

 

In this example, the Arduino drives the Servo motor connected to pin 9. The example moves the servo motor swiftly from 0 degrees to 180 degrees and vice versa. This is a very basic example and gives simple insight into driving the servo motor.

2.  Arduino and Servo motor example – ServoEaser

Project Link:

https://wokwi.com/arduino/projects/300286359425253901

Code and the connection diagram are always available in the link above

Arduino and Servo motor example

 

In this example, the Arduino drives the Servo motor connected to pin 7. The special feature of this code is the function   servoEaser.easeTo( 160, 2000 );

The function accepts two parameters as inputs. The degree to be driven and the time it should take to complete the set rotation angle are given as parameters. In the above example, the servo motor drives to 160 degrees in about 2000 ms (2 seconds). This is a useful example where one needs to have a very smooth movement of the servo with precise time control to complete the rotation.

3.  Arduino and Servo motor example – list of movements

Project Link:

https://wokwi.com/arduino/projects/300287065989317133

Code and the connection diagram are always available in the link above

https://imgur.com/xoTCo5t

Arduino and Servo motor example

In this example, the Arduino drives the Servo motor connected to pin 7. The special feature of this code is that you can specify the list of movements for the servo motor. It is again wrapped into the function used in the earlier example.

The program allows the user to specify the list of moves in an array. The list can be configured based on the need of the project.

4.  Arduino and 2 Servo motors example – time-multiplexing

Project Link:

https://wokwi.com/arduino/projects/300399300324622856

Code and the connection diagram are always available in the link above

https://imgur.com/Y3Lgodw

Arduino and Servo motor example

In this example, the Arduino drives two servo motors. You can drive two servo motors in parallel. You have multiple options. One of the options is time-multiplexing at smaller intervals which is insignificant to the user but is sufficient two drive two servo motors in parallel. At the core level, the control will be switching between two servo motors but the outcome will be smooth, to servo motors running in parallel. You can also change the code, add/modify some logic and see the output instantly on the Wokwi Arduino simulator.

5.  Arduino and 2 Servo motors example – Sync. on the external event

Project Link:

https://wokwi.com/arduino/projects/300399300324622856

Code and the connection diagram are always available in the link above

https://imgur.com/W9x8BER

 

In this example, the Arduino drives two servo motors. One servo motor is connected to pin number 7 and the second one is connected to pin number 6 of the Arduino. In this example, the Arduino will drive one servo motor until the button is pressed. As soon as the button is pressed, the code will sync up both the servo motors at the same angle. This example is very helpful when you need to do some action in synchronisation after an external event. Please refer to the last section in the article to know how to provide your feedback and suggestions.

6.  Arduino and 10 Servo motors example – time-multiplexing

Project Link:

https://wokwi.com/arduino/projects/300480525588496905

Code and the connection diagram are always available in the link above

https://imgur.com/IIRvRRL

Servo Motor free Arduino simulator

In this example, the Arduino UNO operates 10 Servo motors! In this example, the Servo motors are directly connected to the UNO. The current needed by 10 servo motors is huge and this cannot be supplied by the 5V USB. In multiple robotics ARM projects or an octopod project, you will need to drive multiple Servos. You can modify the code to make the project more usable in the multiple arms project. Hit the save button in the Arduino simulator window to save the project and share it with others.

Feedback and suggestions on the Wokwi Arduino simulator

Please feel free to leave your suggestions as comments. You can also hop on to the Discord server of the Arduino simulator if you need any help!