0% found this document useful (0 votes)
59 views11 pages

InfinityX - Electronics Assignment 30.6 Answers-1

The document provides code for controlling a DC motor connected to an Arduino board using two switches. The code defines pin connections for the motor driver, switches, and Arduino. It includes functions for rotating the motor at half or full power in clockwise or counterclockwise directions. When switch 1 is pressed once, the motor runs at half power clockwise, and twice at full power. When switch 2 is pressed, the motor stops for 3 seconds then runs counterclockwise at half or full power depending on presses.

Uploaded by

Radharaman Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views11 pages

InfinityX - Electronics Assignment 30.6 Answers-1

The document provides code for controlling a DC motor connected to an Arduino board using two switches. The code defines pin connections for the motor driver, switches, and Arduino. It includes functions for rotating the motor at half or full power in clockwise or counterclockwise directions. When switch 1 is pressed once, the motor runs at half power clockwise, and twice at full power. When switch 2 is pressed, the motor stops for 3 seconds then runs counterclockwise at half or full power depending on presses.

Uploaded by

Radharaman Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

Answers

Q1. For a simple circuit that consists of 2 switches and 1 dc motor connected to Arduino UNO write a code for the below set of actions

1. If Switch 1 is pressed once the motor should run clockwise direction at 50% of its max speed and if Switch 1 is pressed again it should run at 100% of its
max speed

2. If Switch 2 is pressed once the motor should stop for a time of 3 seconds and must start running anti-clockwise direction at 50% of its max speed and if
Switch 2 is pressed again it should run at 100% of its max speed

Answer :

The answer to the above question goes like this :

➢ First of all, we need an L298N motor driver to be connected to the Arduino.

➢ Connect the motor enable pin (ENABLE A) of the motor driver to the pin no. 9 (PWM pin) on the Arduino.

➢ Connect the motor direction control pins “INPUT 1” and “INPUT 2” of the motor driver to two digital pins 7 and 8 on the Arduino(What we set for
motor direction pin).

➢ We may provide separate power supply to the motor driver or we may provide power supply to both Arduino and Motor driver from a power bank
or a power source of 5 ~ 12 V.

➢ Connect the two terminals of the DC motor to the output terminals “OUT 1” and “OUT 2”(As Enable pin “ENABLE A” is selected) of the motor driver
as per the required direction of the rotation.

➢ Connect one terminal of the push button (Switch 1) to 5V pin of the Arduino and another terminal to the GND pin using a resistor (Avoid current
overload and damage). Also connect the “switch1Pin” or digital pin “2” to that pin (which is connected to resistance and ground).

➢ Connect one terminal of the push button (Switch 2) to 5V pin of the Arduino and another terminal to the GND pin using a resistor. Also connect the
“switch2Pin” or digital pin “3” to that pin (which is connected to resistance and ground).

Schematic Diagram :
2
3

Here is the Code :

// Motor control pins


const int motorEnablePin = 9; // Enable pin for motor (PWM)
const int motorDirectionPin1 = 8; // Motor direction pin 1 (INPUT 1)
const int motorDirectionPin2 = 7; // Motor direction pin 2 (INPUT 2)

// Switch pins
const int switch1Pin = 2; // Connect Switch 1 to digital pin 2
const int switch2Pin = 3; // Connect Switch 2 to digital pin 3

// Variables
int switch1Count = 0; // Number of times Switch 1 is pressed
int switch2Count = 0;

void setup() {
// Motor control pins as outputs
pinMode(motorEnablePin, OUTPUT);
pinMode(motorDirectionPin1, OUTPUT);
pinMode(motorDirectionPin2, OUTPUT);

// Switch pins as inputs with pull-up resistors


pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);

// Initialize the motor as stopped


stopMotor();
}

void loop() {
// Check if Switch 1 is pressed to control motor speed
4

if (digitalRead(switch1Pin) == HIGH) {
switch1Count++;

// Check how many times Switch 1 has been pressed


if (switch1Count == 1) {
rotateHalfPowerClockwise();
} else if (switch1Count == 2) {
rotateFullPowerClockwise();
}

// Reset the switch1PressCount after 2 presses


if (switch1Count >= 2) {
switch1Count = 0;
}
}

// Check if Switch 2 is pressed to stop the motor


if (digitalRead(switch2Pin) == HIGH) {
switch2Count++;

if (switch2Count == 1){
stopMotor();
delay(3000);
rotateHalfPowerCounterclockwise();
} else if (switch2Count == 2) {
rotateFullPowerCounterclockwise();
}
if (switch2Count >= 2) {
switch2Count = 0;
}
}
5

void rotateHalfPowerClockwise() {
digitalWrite(motorDirectionPin1, HIGH); // Set motor direction
digitalWrite(motorDirectionPin2, LOW);
analogWrite(motorEnablePin, 128); // Half power (128 out of 255)
}

void rotateFullPowerClockwise() {
digitalWrite(motorDirectionPin1, HIGH);
digitalWrite(motorDirectionPin2, LOW);
analogWrite(motorEnablePin, 255); // Full power (255 out of 255)
}

void rotateHalfPowerCounterclockwise() {
digitalWrite(motorDirectionPin1, LOW); // Change motor direction (counterclockwise)
digitalWrite(motorDirectionPin2, HIGH);
analogWrite(motorEnablePin, 128); // Half power (128 out of 255)
}

void rotateFullPowerCounterclockwise() {
digitalWrite(motorDirectionPin1, LOW);
digitalWrite(motorDirectionPin2, HIGH);
analogWrite(motorEnablePin, 255); // Full Power 255
}

void stopMotor() {
digitalWrite(motorDirectionPin1, LOW); // Set motor direction to stop
digitalWrite(motorDirectionPin2, LOW);
analogWrite(motorEnablePin, 0); // Disable the motor
}
6

Compiled and Verified in ARDUINO IDE


7

*Q2. The Arduino NANO is one of the most widely used microcontroller boards. Redesign the PCB of the Arduino NANO in the shape of an Infinity with the
following dimensions.

25mm

50mm

Answer :
➢ Here, I have not included the ICSP pins. I have attached a micro USB for uploading Arduino sketch code. I have used CH340 USB to Serial data
converter.

➢ The size of the Arduino PCB is shaped like an infinity with 50mm length and 25mm height as given.
8
9

The Arduino Nano PCB looks like this :


10
11

You might also like