Showing posts with label transistor. Show all posts
Showing posts with label transistor. Show all posts

Sunday, January 31, 2016

How to Reset Your Arduino from Code

In this video we talk about how to reset your Arduino from code with no hands. You can find the schematic and code from this video below.



***************Arduino Code from Video************************************
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
//create a variable for detecting what mode you are in. If used in interrupt must be "volatile"
/*volatile*/ bool nMode = 1;

void setup() {
  pinMode(4, OUTPUT); //set up pin 4, which is connected to base of transistor
  digitalWrite(4,LOW); //set to low so transistor is off and doesn't trigger reset
  pinMode(led, OUTPUT); //set up the LED pin to output
  Serial.begin(57600); //start serial comm
  Serial.println("We just started back up"); //print quick message so we know we just went through setup code
  Serial.println();
  Serial.end(); //need to shut off serial comm because it uses interrupts
  //Create interrupt: 0 for pin 2, the name of the interrupt function or ISR, and condition to trigger interrupt 
  attachInterrupt(0, interruptFunction, CHANGE); 
}

void loop() {
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(400);               // wait 
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(400);               // wait 
  /*
  noInterrupts(); //disables interrupts
  // critical, time-sensitive code here
  interrupts();//enables interrupts
  */
}

//This is the function called when the interrupt occurs (pin 2 goes high)
//this is often referred to as the interrupt service routine or ISR
//This cannot take any input arguments or return anything
void interruptFunction() {
  digitalWrite(4,HIGH); //with this variable set to 1 the LED will blink
 //detachInterrupt(0); this function call will turn the interrupt off
}

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);
}

Tuesday, July 21, 2015

Using a Transistor as a Switch

In this video we cover briefly what a transistor is and discuss how to use them as an electrical switch (DC only). As a demonstration we use an Arduino to switch on and off an NPN transistor and a PNP transistor.



//************************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. This code is open for
//anybody to use or modify

const int BJT = 2; //create variable to control the base of NPN and PNP

void setup() {
  pinMode(BJT, OUTPUT);   // set it as digital output
}

void loop() {
  digitalWrite(BJT, LOW);   // set base of NPN and PNP to low
  delay(1000);
  digitalWrite(BJT, HIGH);   // set base of NPN and PNP to high
  delay(1000);
}