0% found this document useful (0 votes)
147 views2 pages

PWM DC With RPWM & LPWM New

The code uses a potentiometer and two toggle switches to control the speed and direction of a DC motor connected to an Arduino. It maps the potentiometer reading to PWM values for the right and left motor pins to control motor speed with the pot. The toggle switches determine whether the right motor runs alone, the left motor runs alone, or both run in the same direction based on the pot reading.

Uploaded by

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

PWM DC With RPWM & LPWM New

The code uses a potentiometer and two toggle switches to control the speed and direction of a DC motor connected to an Arduino. It maps the potentiometer reading to PWM values for the right and left motor pins to control motor speed with the pot. The toggle switches determine whether the right motor runs alone, the left motor runs alone, or both run in the same direction based on the pot reading.

Uploaded by

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

Purpose of the code is to just use single Voltage DC current at variable PWM in AC

DC Mode

int potPin = A0; // center pin of the potentiometer


int togglePin = 12; //toggle switch A
int togglelPin = 13; //toggle switch B
int rightPin = 5; // Arduino Right PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int leftPin = 6; // Arduino Left PWM output pin 6; connect to IBT-2 pin 1 (LPWM)
int potValue = 0; //value read from pot
int rightValue = 0;//value of motor pin
int leftValue = 0; //value of left motor pin

void setup()
{
Serial.begin(9600);
pinMode(potPin, INPUT);
pinMode(togglePin, INPUT);
pinMode(togglelPin,INPUT);
pinMode(rightPin, OUTPUT);
pinMode(leftPin, OUTPUT);
}

void loop()
{
potValue = analogRead(potPin); //reading from potentiometer
rightValue = map(potValue, 0, 1023, 0, 255);
leftValue = map(potValue, 0,1023,0,255);

if (digitalRead(togglePin == HIGH && digitalRead(togglelPin) == LOW)

{
digitalWrite(rightPin, HIGH);
digitalWrite(leftPin, LOW);
}
else if (digitalRead(togglePin == LOW && digitalRead(togglelPin) == HIGH)
{
digitalWrite(rightPin, LOW);
digitalWrite(leftPin,HIGH);
delay (500);
digitalWrite(rightPin,HIGH);
digitalWrite(leftPin,LOW);
delay (500);
}
else (digitalRead(togglePin == LOW && digitalRead (togglelPin) == LOW)
{
digitalWrite(rightPin, LOW);
digitalWrite(leftPin,LOW);

analogWrite(rightPin, rightValue);
analogWrite(leftPin, leftValue);

// print the results to the Serial Monitor:


Serial.print("Pot = ");
Serial.print(potValue);
Serial.print("\t output = ");
Serial.println(rightValue);
Serial.println(leftValue);

// wait 2 milliseconds before the next loop for the analog-to-digital


// converter to settle after the last reading:
delay(2);
}

You might also like