Components

>>> from zero.components import Resistor, Capacitor, Inductor, OpAmp

What is a ‘component’?

A component represents a circuit device which sources or sinks current, and produces voltage drops between its nodes. Passive components such as resistors, capacitors and inductors do not produce or amplify signals, but only apply an impedance to their input. Active components such as op-amps can source current.

Instantiated components may be added to circuits using add_component(); however, the methods add_resistor(), add_capacitor(), add_inductor() and add_opamp() allow components to be created and added to a circuit at the same time, and avoid the need to import them directly.

Note

The recommended way to add components to a circuit is to use the add_resistor(), add_capacitor(), add_inductor() and add_opamp() methods provided by Circuit. These offer the same functionality as when creating component objects directly, but avoid the need to directly import the component classes into your script.

Component names

Components may be provided with a name on creation using the name keyword argument, i.e.

>>> r = Resistor(name="r1", value="430k", node1="n1", node2="n2")

or

>>> from zero import Circuit
>>> circuit = Circuit()
>>> circuit.add_resistor(name="rin", value="430k", node1="n1", node2="n2")

Names can also be set using the name property:

>>> r.name = "r1"

Component names can be used to retrieve components from circuits:

>>> r = circuit["rin"]
>>> print(r)
rin [in=n1, out=n2, R=430.00k]

Component names must be unique within a given circuit. When trying to add a component to a circuit where its name is already used by another circuit component, a ValueError is raised.

Note

Component names do not need to be unique within the global namespace. That means components with different values or nodes can have the same name as long as they are not part of the same circuit.

Naming of components is not required; however, when a component is added to a circuit it is assigned a name if it does not yet have one. This name uses a prefix followed by a number (the lowest positive integer not resulting in a name which matches that of a component already present in the circuit). The character(s) used depend on the component type:

Component

Prefix

Example

Resistor

r

r1

Capacitor

c

c1

Inductor

l

l1

Op-amp

op

op1

Setting a component’s value

A passive component’s value may be altered. First, get the component:

c1 = circuit["c1"]

You can then set the value using the object’s value attribute:

c1.value = "1u"

In the above example, the string is parsed parsed by Quantity into an appropriate float representation. You may also specify a float or int directly:

c1.value = 1e-6

You may also provide a string with units or scales:

# Quantity with scale factor and unit.
c1.value = "2.2nF"

The above value is parsed as 2.2e-9, with unit F. The unit is stored alongside the numeric part within the object, and the unit will be printed alongside the component’s value when it is displayed.

Note

Units are just for display and are not used for any calculations. Be careful when specifying units which differ from those used internally by Zero.