Potentiometer Integrated with On/Off Motor Switch

Hi all,

I have been able to create a button that works as an on/off switch for my DC motor, but when I add in code to get the potentiometer to function, it just messes the whole thing up. (I also have to keep the batteries on to power the motor - I can't just use the arduino, its a requirement.) If you have any tips or trips that'd be great. I'm not sure I am using the correct loop set up, as I am fairly new to Arduino. Below is a code and schematic. The code I was initially using to get the potentiometer to work without the button begins were it says

  c2= analogRead(pot);

Thank you in advance!!

// This is the code for the "Fan":
bool motor=0;
int pwmPin = 12; // assigns pin 12 to variable pwm
int pot = A0; // assigns analog input A0 to variable pot
int c1 = 0;   // declares variable c1
int c2 = 0;   // declares variable c2

void setup()
  pinMode(12, OUTPUT);
  pinMode(9, INPUT);
void loop()
 int push=digitalRead(9);
 if (push==0)   
  c2= analogRead(pot); 
  c1= 1024-c2;         // subtracts c2 from 1000 ans saves the result in c1
  digitalWrite(12, HIGH); 
  digitalWrite(12, LOW);  

What is that curious component connecting the transistor collector to the 5 V logic supply?

No, can't be a bipolar transistor anyway as there is no base resistor. :astonished:

there is no base resistor allright, and there should be, at least 1k, he connencted the colector/or source to the motor so his transistor is after the motor that draws from 9V, while emitter/ drain is grounded, and the capacitor is for grounding transients (i think), antiparalel diode or cap across the motor would be better i think? why not read pot value with analog read and then map it to analog write function=? using pwm.

Paul, if its a mosfet does arduino puldown the gate on its own? shouldnt one put a puldown to the gate, and a small gate resistor too?

Paul, if its a mosfet does arduino puldown the gate on its own? shouldn't one put a puldown to the gate, and a small gate resistor too?

As discussed here just earlier today, the pull-down matters if you care what happens on startup - or in the case of an extended "reset" condition (crash), having the FET partially conducting and overheating - and the series resistor is arguably necessary if PWM is used.

Thank you guys for your insight! I am fairly new to Arduino, it is for a class and with COVID19, its online and been a lot of self-learning lately. I honestly did not know I needed another resistor with the transistor, so I will add that in.

if you care

Aha, I used arduino only to drive a pretty hefty inverter, so I definitely cared enough to put one anyway :slight_smile: