I have just a little problem

So, i wanted to improvise, since i just saw how a type of tranasistor works with DC motor. First, i dont know why the code is not printing the initial value of my pir motion sensor, and, how does this vibration motor work anyways

this is my code:

int pir = 0;
void setup()
{
  pinMode(3, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(13, INPUT);
  Serial.begin(9600);
}

void loop()
{
  pir = digitalRead(13);
  Serial.println(pir);
  
  if(pir == 1){
  digitalWrite(3, HIGH);
  digitalWrite(1, HIGH);
    delay(1000);
  }else{
    digitalWrite(3, LOW);
  digitalWrite(1, LOW);
    delay(1000);
}
}

please, please never ever again post pictures of text... Not only they are not readable nor usable directly for copy&paste but they use up lots of storage and internet bandwidth which contributes to polluting the planet.

➜ do your part and do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).


pin #1 is Tx of Serial, you don't want to use it ...

.. and include an electrical diagram along with part numbers. Especially for the transistor

Google kickback diode.

i just did. i dont understand what it has to do with the post

Data sheets or other documentation for each component will also be very helpful.

The motor is missing the required flyback diode to protect the MOSFET and some optional resistors to limit gate charge current and keep the MOSFET turned off while the gate is floating. The cap will bypass brush noise and help to keep the power clean.

sorry, im kinda new to this. i really don't get what you just said

sorry about that

note this too

it's not too late to edit your first post and replace the code image by actual code using code tags... (hint)

As @groundFungus shows, add a reverse biased diode (kickback diode) across the motor.

Also in the circuit you can see two resistors on the input of the N channel MOSFET, missing in your circuit.

Also make sure you are using a logic level MOSFET.

i've edited the post. ill change pin 1 now. And, what do you mean by Tx of serial?

also, the vibration motor is working now, but it's doing the reverse. when motion is not detected, it is vibrating, but when motion is detected, its not vibrating

you are missing the code tags, please read how to use the forum and post code correctly

Edit your post using the :pencil2: in the tool bar just below your post

select the code part and press the <code/> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlt on a PC or cmdt on a Mac)

that's what you'll see if you look at your board up close

image

I mean pin #1 is the Tx pin of your Serial instance (it's used by Serial to transmit bits to your Serial monitor)

I guess that you did not understand how to properly post code.

First use Use the IDE autoformat tool (ctrl-t or Tools, Auto format) to format the code.

The post the code according to the instructions in the forum guidelines.

It will look like this:



int pir = 0;

void setup()
{
   pinMode(3, OUTPUT);
   pinMode(1, OUTPUT);
   pinMode(13, INPUT);
   Serial.begin(9600);
}

void loop()
{
   pir = digitalRead(13);
   Serial.println(pir);

   if (pir == 1)
   {
      digitalWrite(3, HIGH);
      digitalWrite(1, HIGH);
      delay(1000);
   }
   else
   {
      digitalWrite(3, LOW);
      digitalWrite(1, LOW);
      delay(1000);
   }
}

You need to invert the operation in the code.

Is that little round black thing an ultra sonic speaker ?

What ever it is, it is connected between 6V and the Arduino output pin, never do this.

i've done that. thanks

just did that. thanks

it's a vibration motor. So, what do i do?

You can possibly use a BJT as a driver, a MOSFET is a more modern/better choice.

What is the resistance of this vibrating motor ?

BTW, is there a reason why you don’t want to parallel the two motors (you seem to want to drive them with separate pins :thinking: ) ?

ok.

i did put any resistance. the two terminals are connected to battery and pin, i am just starting to learn arduino, i saw a video on this motor, and decided to put another one just to improvise.

i think itll be better to use same pins then. also, in the video i watched, i didnt get why we are using a transistor, the guy skipped it