Sunday, August 2, 2015

Building a Not Gate from a Transistor

In this video we take a brief look at how a transistor works and how to use it as a not gate or an digital logic inverter. We look at an example with Arduino using both an NPN and a PNP bipolar junction transistor as a not gate



//***********Arduino Code ******************************************
//This example code was used on the Forcetronics YouTube Channel to demonstrate how to 
//make an inverter or not gate using an NPN and PNP transistor

const int NPN = 2; //create variable for NPN base 
const int PNP = 3; //create variable for PNP base
const int NPNRead = 4; //create variable to read NPN
const int PNPRead = 5; //create variable to read PNP

void setup() {
  pinMode(NPN, OUTPUT);   // set pin to output
  pinMode(PNP, OUTPUT);   // set pin to output
  pinMode(NPNRead, INPUT);   // set pin to input
  pinMode(PNPRead, INPUT);   // set pin to input
  Serial.begin(9600); //start serial comm
}

void loop() {
  digitalWrite(NPN, LOW);   // set NPN base to low
  digitalWrite(PNP, LOW);   // set PNP base to low
  Serial.println("Base of NPN and PNP is set to Low");
  Serial.print("NPN reads ");
  Serial.println(digitalRead(NPNRead)); //read and print value at NPN digital pin
  Serial.print("PNP reads ");
  Serial.println(digitalRead(PNPRead)); //read and print value at PNP digital pin
  Serial.println();
  delay(1000);
  digitalWrite(NPN, HIGH);   // set NPN base to high
  digitalWrite(PNP, HIGH);   // set PNP base to high
  Serial.println("Base of NPN and PNP is set to High");
  Serial.print("NPN reads ");
  Serial.println(digitalRead(NPNRead)); //read and print value at NPN digital pin
   Serial.print("PNP reads ");
  Serial.println(digitalRead(PNPRead)); //read and print value at PNP digital pin
  Serial.println();
  delay(1000);
}