Cutoff power of Servo using transistor and Interrupt

Hello everyone,

I need your help about an issue. I am trying to control a servo inside of the locking mechanism.

Firstly i need to press a button and after that servo's power need to cutoff after it turned because if servo has power, cant be controlled by a key due to stall torque of servo.

Therefore, i designed a circuit using NPN Transistor and i had to use interrupt (
my teacher's request ).

I am sharing the codes and screenshots :

#include <Servo.h>
Servo servo;

int transistorPin = 5;
int butonPin = 2;

void setup() {

  pinMode(butonPin, INPUT);
  pinMode(transistorPin, OUTPUT);
    
  digitalWrite(transistorPin, HIGH);
  servo.attach(3);
  
  servo.write(0);
  delay(2000); //motorun dönmesi için gereken süre..
  
  attachInterrupt(digitalPinToInterrupt(butonPin),lock_manual,FALLING);
  Serial.begin(9600);
  
  digitalWrite(transistorPin, LOW);  
}


void loop(){}
 
void lock_manual(){
  Serial.println("Butona basildi");
  digitalWrite(transistorPin, HIGH);
  servo.write(180);
  delay(2000);
  //digitalWrite(transistorPin, LOW);        
}

devre

Issue is that, if i add this code //digitalWrite(transistorPin, LOW); the servo doesn't turn but if i don't add this code the servo turns but power doesn't cutoff..

What should i do ?

Hi @zakk_wylde , please post a schematic of your project.

RV mineirin

1 Like

@ruilviana updated..

@zakk_wylde, your topic has been moved to a more suitable location on the forum.

===

FYI, I still don't see a schematic :wink:

I guess the image does not show. I am sharing as link. https://i.hizliresim.com/75vktr2.png

Hi,

What your image does not show is the pin labels of the transistor or what type it is.
Using an NPN transistor to high side switch is not the best way to do it, you should try a PNP type.
You also need a series base resistor.
A proper schematic will show a clearer and more informative project.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I used lots of transistor and relay. Result was always the same. So, i didn't need to write the transistor name..

NPN BC237
220 Ohm Base Resistor
MG90s Servo Motor

and even i used a relay before instead of transistor.

Single channel 5V

Results are always same..

If i remove digitalWrite(transistorPin, LOW) it working. Servo is rotating but not de-energized.

@TomGeorge

Hi,
Please show the collector ,base, and emitter connections.
A BC237 is only rated at 100mA, a servo pulls more than 10 times that amount when starting.

Do you have a DMM?

Have you actually built this in the REAL WORLD?

Tom.... :grinning: :coffee: :coffee: :australia:

I don't have to use a transistor. Just i want to solve this problem :smiley: When pressed the button using Interrupt servo should rotate and after that power of servo cut off..

How can i solve it. I can buy all the new parts to solve this problem..

By the way, in fact all the connections are clearly visible in the images even collector emitter base.

The left leg of the transistor is the collector, the right leg is the emitter, and the middle leg is the base.

I created the circuit in the real life results are same again.

Hi,
Why do you need to use an interrupt?

Tom.. :grinning: :+1: :coffee: :australia:

because my teacher wants it to be like this. :confused: @TomGeorge

Hi,
Try this code.
See if the simulator will work.

#include <Servo.h>
Servo myservo;

int transistorPin = 5;
int butonPin = 2;
bool butonState;
void setup()
{
  pinMode(butonPin, INPUT);
  pinMode(transistorPin, OUTPUT);
  digitalWrite(transistorPin, HIGH);
  myservo.attach(3);
  myservo.write(0);
  delay(2000); //motorun dönmesi için gereken süre..
  Serial.begin(9600);
  digitalWrite(transistorPin, LOW);
}


void loop()
{
  butonState = digitalRead(butonPin);
  if (butonState == LOW)
  {
    digitalWrite(transistorPin, HIGH);
    myservo.write(180);
  }
}

Then think about interupts.
Tom... :grinning: :+1: :coffee: :australia:

It worked. Motor has rotated.

Everything is normal in the loop by the way. If i use interrupt it won't work. @TomGeorge

Hi,
Try this, you try not to do too much in an interrupt function, just flag that it has happened.
It compiles, but its not tested.

#include <Servo.h>
Servo myservo;

int transistorPin = 5;
int butonPin = 2;
bool butonState;
bool lockState = false;

void setup()
{
  pinMode(butonPin, INPUT);
  pinMode(transistorPin, OUTPUT);
  digitalWrite(transistorPin, HIGH);
  myservo.attach(3);
  myservo.write(0);
  delay(2000); //motorun dönmesi için gereken süre..
  Serial.begin(9600);
  digitalWrite(transistorPin, LOW);
  attachInterrupt(digitalPinToInterrupt(butonPin), lock_manual, FALLING);
  Serial.println("==SETUP COMPLETE==");
  Serial.println("== DOOR UNLOCKED ==");
}

void loop()
{
  if (lockState == true)
  {
    digitalWrite(transistorPin, HIGH);
    myservo.write(180);
    Serial.println("== DOOR LOCKED ==");
  }
  
}
//===================================
void lock_manual()
{
  lockState = true;
}

Tom... :grinning: :+1: :coffee: :australia:

It worked too :slight_smile:

Hi,
Do you see how it works?
You may need to add code to turn the power OFF to the servo after you have activated the lock.

In the real world, the transistor would not have been suitable for that application.

Tom... :grinning: :+1: :coffee: :australia:

Can you add code to turn the power OFF the servo ? Because i know if i add code it won't work :smiley:

and what parts should i use in the real life ?

Hi,
Its as simple as this;

#include <Servo.h>
Servo myservo;

int transistorPin = 5;
int butonPin = 2;
bool butonState;
bool lockState = false;

void setup()
{
  pinMode(butonPin, INPUT);
  pinMode(transistorPin, OUTPUT);
  digitalWrite(transistorPin, HIGH);
  myservo.attach(3);
  myservo.write(0);
  delay(2000); //motorun dönmesi için gereken süre..
  Serial.begin(9600);
  digitalWrite(transistorPin, LOW);
  attachInterrupt(digitalPinToInterrupt(butonPin), lock_manual, FALLING);
  Serial.println("==SETUP COMPLETE==");
  Serial.println("== DOOR UNLOCKED ==");
}

void loop()
{
  if (lockState == true)
  {
    digitalWrite(transistorPin, HIGH);
    myservo.write(180);
    Serial.println("== DOOR LOCKED ==");
    delay(2000);          // Allow time for servo to move.
    digitalWrite(transistorPin, LOW);
  }

}
//===================================
void lock_manual()
{
  lockState = true;
}

You will need to add more comments to show you know what the code is doing.

Tom... :grinning: :+1: :coffee: :australia:

Yeah but it didn't work again.. :slight_smile: LOW doesn't work.

#include <Servo.h>
Servo myservo;

int transistorPin = 5;
int butonPin = 2;
bool butonState;
bool lockState = false;

void setup()
{
  pinMode(butonPin, INPUT);
  pinMode(transistorPin, OUTPUT);
  digitalWrite(transistorPin, HIGH);
  myservo.attach(3);
  myservo.write(0);
  delay(2000); //motorun dönmesi için gereken süre..
  Serial.begin(9600);
  digitalWrite(transistorPin, LOW);
  attachInterrupt(digitalPinToInterrupt(butonPin), lock_manual, FALLING);
  Serial.println("==SETUP COMPLETE==");
  Serial.println("== DOOR UNLOCKED ==");
}

void loop()
{
  if (lockState == true)
  {
    digitalWrite(transistorPin, HIGH);
    myservo.write(180);
    Serial.println("== DOOR LOCKED ==");
    delay(2000);          // Allow time for servo to move.
    digitalWrite(transistorPin, LOW);
if (transistorPin == LOW){
    Serial.println("Power cut off.");
  }
  }

}
//===================================
void lock_manual()
{
  lockState = true;
}

I added a if condition to learn power's state.