Handout-Control Using a Microcontroller
Handout-Control Using a Microcontroller
Tasks: Perform the following control tasks using an ARDUINO microcontroller. Make the
circuit for each task, write an Arduino sketch, compile it, upload it to the microcontroller,
and verify and demonstrate the desired control action.
• The ARDUINO codes for the first three tasks are given below so that you can learn
ARDUINO programming quickly and successfully implement these tasks.
• Watch my video on “Control using a Microcontroller”, which explains how to make
Arduino circuits for the first three tasks, the logic behind the codes below, and how
to implement these tasks.
• Learn the logic used, make files (i.e., Arduino sketches using these codes) on your
Laptop in IDE, upload them to the board, and demonstrate and verify the desired
control actions.
Report: Prepare a report with the a) circuit diagram, b) code listing, c) photo of the setup
you made and of the setup in action, and d) a brief description of the logic used to make
the code for each task
****
Control theory and applications (MCL212): Prof. S. V. Modak, IIT Delhi
ARDUINO codes
Control blinking of two LEDs in sequence. ON time = 2000 ms and OFF time = 500 ms.
The LEDs should continue to blink in sequence once started.
Further tasks: Change the ON/OFF times; change the delay. How will you modify the
program to blink the LEDs only a certain number of times?
//*************************************************
// THIS PROGRAM BLINKS TWO LEDs IN SEQUENCE
//*************************************************
//Define global constants here
int out_pin_1=12;
int out_pin_2=10;
int OFF_time=500;
int ON_time=2000;
//*************************************************
void setup() {
// put your setup code here, to run once:
pinMode(out_pin_1,OUTPUT);
pinMode(out_pin_2,OUTPUT);
}
//*************************************************
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(out_pin_1,HIGH);
delay(ON_time);
digitalWrite(out_pin_1,LOW);
delay(OFF_time);
digitalWrite(out_pin_2,HIGH);
delay(ON_time);
digitalWrite(out_pin_2,LOW);
delay(OFF_time);
}
//*************************************************
Control theory and applications (MCL212): Prof. S. V. Modak, IIT Delhi
Here, the brightness of the LEDs is controlled by a potentiometer and PWM signal.
//*************************************************
// THIS PROGRAM BLINKS TWO LEDs TOGETHER PROPORTIONAL TO THE
// POTENTIOMETER INPUT AND A PWM SIGNAL.
//*************************************************
//*************************************************
void setup() {
// put your setup code here, to run once:
pinMode(out_pin_1, OUTPUT);
pinMode(out_pin_2, OUTPUT);
Serial.begin(9600);
}
//*************************************************
void loop() {
// put your main code here, to run repeatedly:
analogWrite(out_pin_1, int_val_voltage_255);
analogWrite(out_pin_2, int_val_voltage_255);
}
//*************************************************
//********************************************************
// THIS PROGRAM ROTATES A SERVO MOTOR THROUGH DESIRED
ANGLES
//********************************************************
// INCLUDE INBUILT SERVO LIBRARY
#include <Servo.h>
int out_pin_1=9;
Servo servo1;//DECLARE A SERVO OBJECT WITH A CHOSEN NAME
//*************************************************
void setup() {
// put your setup code here, to run once:
servo1.attach(out_pin_1);
}
//*************************************************
void loop() {
}
//*************************************************
d) Blink the two LEDs in breathing mode using the PWM signal
Here, the LEDs should again blink in sequence, but the intensity of brightness of the two
LEDs should be increased from 0 to maximum and back to 0. Flash the inputs delivered
to the LEDs on the serial monitor.
****