Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15758

Controlling DC Motor with Mosfet, Potentiometer and Relay

$
0
0

I am trying to control the speed of a DC Motor using a Potentiometer and Mosfet while at the same time using a Relay and an IR Receiver to switch the power on and off to the Motor. I attach a Picture of my circuit.Instead of the 9V Battery i am using a 9V power supply. Also not sure if i connected the relay correctly in the circuit i drawed because i couldnt find the same relay i am atually using. Relay i am using : https://www.amazon.de/-/en/dp/B07BVXT1ZK?psc=1&ref=ppx_yo2ov_dt_b_product_details. I have connected the - of the Relay to the negative of the Breadboard and the + to the positive and the signal to Pin Nr. 11 of my Arduino.

With the help of chat GPT and Google i created the following code:

#include <IRremote.h>

const int IR_RECEIVER_PIN = 4;    // Pin for the IR receiver module
const int RELAY_PIN = 11;         // Pin connected to the relay
const int potPin = A0;            // Analog input pin for potentiometer
const int mosfetGatePin = 9;     // PWM-capable digital output pin connected to the MOSFET gate

uint32_t last_decodedRawData = 0;

IRrecv irrecv(IR_RECEIVER_PIN);
decode_results results;

void translateIR() // takes action based on IR code received
{
  // Check if it is a repeat IR code 
  if (irrecv.decodedIRData.flags)
  {
    //set the current decodedRawData to the last decodedRawData 
    irrecv.decodedIRData.decodedRawData = last_decodedRawData;
    Serial.println("REPEAT!");
  } 
  else
  {
    //output the IR code on the serial monitor
    Serial.print("IR code:0x");
    Serial.println(irrecv.decodedIRData.decodedRawData, HEX);
  }
  //map the IR code to the remote key
  switch (irrecv.decodedIRData.decodedRawData)
  {
    case 0xF30CFF00: digitalWrite(RELAY_PIN, HIGH);    break;
    case 0xE718FF00: digitalWrite(RELAY_PIN, LOW);    break;
    default:
      Serial.println(" other button   ");
  }// End Case
  //store the last decodedRawData
  last_decodedRawData = irrecv.decodedIRData.decodedRawData;
  delay(500); // Do not get immediate repeat
} //END translateIR

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();  // Start the IR receiver
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(mosfetGatePin, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  // Set initial state to HIGH (off)
}

void loop() {
  if (irrecv.decode()) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }

  // Read the analog value from the potentiometer
  int potValue = analogRead(potPin);

  // Map the potentiometer value to the PWM duty cycle range (0-255)
  int motorSpeed = map(potValue, 0, 1023, 0, 255);

  // Control the speed of the motor using PWM
  analogWrite(mosfetGatePin, motorSpeed);

  // Add a short delay to prevent rapid changes
  delay(100);
}

Before Pluging anything and possibly damaging my Hardware, should i change anything and would the Circuit work? P.S i am using Buz12 Mosfet 10K Potentiometer and 10K Resistor

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15758

Latest Images

Trending Articles



Latest Images