Skip to content

Instantly share code, notes, and snippets.

@dmgcodevil
Created November 21, 2023 22:41
Show Gist options
  • Select an option

  • Save dmgcodevil/e72596caccb25c54dce48b5f76929641 to your computer and use it in GitHub Desktop.

Select an option

Save dmgcodevil/e72596caccb25c54dce48b5f76929641 to your computer and use it in GitHub Desktop.
RC,RE,RB1,RB2 calculator for Class A NPN Common-Emitter Amplifier
import scala.collection.mutable
// RC,RE,RB1,RB2 calculator for Class A NPN Common-Emitter Amplifier
object Main {
val I_DIVIDER_GAIN = 10 // I0 > IB
def main(args: Array[String]): Unit = {
val Vcc = 19.5
val inputs = List(
Input(label = "2N3904", vcc = Vcc, hfe = 100, vbe = 0.7, icq = 0.001, ve = 2),
Input(label = "TIP122", vcc = Vcc, hfe = 1000, vbe = 1.2, icq = 0.1, ve = 2),
)
inputs.foreach { in =>
val r = calc(in)
println("Input:")
println(in)
println("Result:")
println(r)
println("=" * 10)
}
}
/*
A current I0 goes through resistors R1 and R2 and a current Ib just goes through R1 and enters the base
from the connection with R1 and R2. Conservation of current allows us to conclude the current in R1 is
the sum of these currents that is (I0 + Ib).
*/
/*
+---[ R1 ]---+---[ R2 ]---+
| | |
I0+Ib Ib I0
| | |
+------------+------------+
|
V_battery
-
*/
def calc(input: Input): Result = {
val ic = input.icq
val ib = input.icq / input.hfe
val vrc = input.vcc / 2
val vb = input.vbe + input.ve
val re: Int = (input.ve / input.icq).intValue
val rc: Int = (vrc / ic).intValue
// bias voltage divider
// (I0+IB)*R1 + I0*R2 = VCC
// VBE+VE = I0*R2
// (I0+Ib)*R1 + (VBE+VE) = VCC
// VB = VBE+VE
// (I0+IB)*R1 + VB = VCC
// (I0+IB)*R1 = VCC-VB
// I0 = I_DIVIDER_GAIN * IB
// R1 = (VCC-VB) / (I0+IB)
// neglect IB since IB << I0
// R1 = (VCC-VB) / I0
val i_divider = I_DIVIDER_GAIN * ib // I0
val rb1: Int = ((input.vcc - vb) / i_divider).intValue
// VBE+VE = I0*R2
// VB = I0*R2
// R2 = VB / I0
val rb2: Int = (vb / i_divider).intValue
Result(
vb = vb,
re = re,
rc = rc,
rb1 = rb1,
rb2 = rb2
)
}
/**
*
* @param label some information
* @param vcc power source (V)
* @param hfe transistor gain (100-1000)
* @param vbe base-emitter voltage (V)
* @param icq quiescent current (A)
* @param ve emitter voltage, 10% of vcc (V)
*/
case class Input(label: String,
vcc: Double,
hfe: Int,
vbe: Double,
icq: Double,
ve: Double) {
override def toString: String = {
val sb = new mutable.StringBuilder()
sb.append("LABEL=").append(label).append("\n")
sb.append("VCC=").append(vcc).append("\n")
sb.append("hFE=").append(hfe).append("\n")
sb.append("VBE=").append(vbe).append("\n")
sb.append("ICQ=").append(icq).append(" A\n")
sb.append("VE=").append(ve).append("\n")
sb.toString()
}
}
/**
*
* @param vb base voltage
* @param re emitter resister
* @param rc collector resister
* @param rb1 voltage divider resister 1
* @param rb2 voltage divider resister 2
*/
case class Result(vb: Double,
re: Int,
rc: Int,
rb1: Int,
rb2: Int) {
override def toString: String = {
val sb = new mutable.StringBuilder()
sb.append("VB=").append(vb).append("\n")
sb.append("RE=").append(re).append("\n")
sb.append("RC=").append(rc).append("\n")
sb.append("RB1=").append(rb1).append("\n")
sb.append("RB2=").append(rb2).append("\n")
sb.toString()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment