- AR-B1: Blinking LED Project using Arduino
- AR-B2: Alternate Control of LED & Buzzer
- AR-B3: Alternate Blinking LEDs
- AR-B4: Running Effect of LEDs
- AR-B5: Chasing Effect of LEDs
- AR-B6: Obstacle Detector using IR Sensor
- AR-B7: Object Counter using Serial Monitor
- AR-B8: Servo Motor Basic Code
- AR-B10: Car Wiper System using Servo Motor
- B13: LED Display Down Counter with Alarm using Arduino
- Protected: Day-1: Online Course on Arduino Programing with Practicals
- Protected: Day-2: Online Course on Arduino Programing with Practicals
- Protected: Day-3: Online Course on Arduino Programing with Practicals
Important Note: In this series, the previous project #B9 is removed due to similar content like B8.
In this code when the button is pressed, the servo motor starts rotating smoothly from 0 – 90 and then 90 – 0. When the button is released, the motor restores back to angle 0.
The Code
Now let us write the code for this project. We will understand each command line in it as given in comments in the code.
#include <Servo.h> Servo i; int angle = 0; int sw = 7; // connect button between pin-7 and ground void setup() { i.attach(3); pinMode(sw,INPUT_PULLUP); i.write(0); } void loop() { while(digitalRead(sw)==LOW) { for(angle=0;angle<100;angle++) { i.write(angle); delay(5); } delay(100); for(angle=100;angle>0;angle--) { i.write(angle); delay(5); } delay(100); } }
Circuit Diagram
Important: In some cars the sweeping angle of wipers may be up to 100 degree. So you can change the value of 90 degree to 100 degree.
For more interesting circuits to build with Arduino UNO and Arduino Nano or even Arduino Mega, join our course of Arduino Robotics to learn complete programming techniques to construct any type of project in your mind.