0
\$\begingroup\$

I'm New in electronics. I read "Robot Building for Beginners" book and I'm trying to make a line follower circuit. Instead of LM393 and 2N2907A I'm using arduino nano and 2N2222A. Here's my code:

const int leftPin = 9;
const int rightPin = 2;

int leftSensor;
int rightSensor;

bool switchLine = HIGH;

void setup() {
  pinMode(11, INPUT_PULLUP);

  pinMode(rightPin,OUTPUT);
  pinMode(leftPin,OUTPUT);
  digitalWrite(rightPin,LOW);
  digitalWrite(leftPin,LOW);
}

void loop() {
  switchLine = digitalRead(11);

  leftSensor = analogRead(6);
  rightSensor = analogRead(5);

  if(leftSensor > rightSensor)
  {
    if(switchLine)
    {
      digitalWrite(rightPin,LOW);
      digitalWrite(leftPin,HIGH);
    } 
    else
    {
      digitalWrite(rightPin,HIGH);
      digitalWrite(leftPin,LOW);
    };
  }

  if(leftSensor < rightSensor)
  {
    if(switchLine) 
    {
      digitalWrite(rightPin,HIGH);
      digitalWrite(leftPin,LOW);
    } 
    else
    {
      digitalWrite(rightPin,LOW);
      digitalWrite(leftPin,HIGH);
    };
  }
  delay(20);
}

And here's my circuit (I did not draw other parts of the circuit because they work fine):

schematic

simulate this circuit – Schematic created using CircuitLab

If I connect one LED without transistor to arduino output it works, if I connect base of transistor to positive terminal three LEDs light up, but when I connect base to arduino it does not work. Multimeter shows there is 5V on base of the transistor but no current.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

what is the vf of the LED’s. if you want to turn on the transistor , it base should be around 0.7 V above emitter. that means the output of the MCU should be (0.7 + (3x Vf_LED)).

\$\endgroup\$
7
  • 4
    \$\begingroup\$ In other words: to get this to work, put the LEDs and the resistor in the collector wire, not in the emitter wire. \$\endgroup\$ Jul 29, 2018 at 12:02
  • \$\begingroup\$ LEDs are 5mm yellow and green, if vf means forward voltage I think it's about 2V. \$\endgroup\$
    – Sasan
    Jul 29, 2018 at 12:06
  • \$\begingroup\$ so that explains. the easiest thing to do is what @wouter says. Move the transistor to the low side; that is have transistor emitter to gnd and collector to cathode of the third LED. And thus the first LED’s connected to VCC \$\endgroup\$
    – Navaro
    Jul 29, 2018 at 12:10
  • \$\begingroup\$ The transistors are supposed to run motors too, I can't put LEDs on collector. I reduces LEDs to two and now they work, Thanks guys \$\endgroup\$
    – Sasan
    Jul 29, 2018 at 12:14
  • 2
    \$\begingroup\$ Motors? Also at the emitter?? Poor transistors... If you have a problem, always give the full context! \$\endgroup\$ Jul 29, 2018 at 12:26
7
\$\begingroup\$

if I connect base of transistor to positive terminal three LEDs light up

Yes, that'll probably work despite the transistor being wired as an emitter follower.

but when I connect base to arduino it does not work. Multimeter shows there is 5V on base of the transistor but no current.

Because the transistor is wired as an emitter follower - the emitter "follows" the base voltage but about 0.7 volts lower. So with 5 volts on the base there will be about 4.3 volts at the emitter and each LED will share that voltage because they are in series.

That's approximately 1.43 volts across each LED and, unlesss they are infra red devices (requiring a lower forward voltage to illuminate) you won't see any glow.

The most appropriate way to drives LEDs in a situation like this is to put them in the collector like this: -

enter image description here

Now the BJT fully turns on to virtually a short circuit (maybe 100 mV across it) and there is almost the full 9 volts across the current limiting resistor and LEDs. It's usually the most appropriate way to simply drive motors too except i would consider using an N channel MOSFET instead of a BJT.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your answer. I did everything as the book said. But since I didn't have LM393 and 2N2907A I went with arduino and 2N2222A. Two or Three LEDs are not a big deal, there is a motor parallel to each LED line and they move the robot based on reading from photoresistors connected to analog pins. \$\endgroup\$
    – Sasan
    Jul 29, 2018 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.