# Electronics

## Does the SAMD21 have pullups on every pin?

Yes, the ATSAMD21 family of microcontrollers has internal pull-ups (and pull-downs) on every Port A / Port B pin (like PB03 or PA14) that you can enable or disable in software.

The RESET pin also has an internal pull-up which is always active and can’t be disabled.

Posted by Uli Köhler in Electronics

## Which pins can be used as external interrupts on the SAMD21?

The SAMD21 family of microcontrollers features the EIC (External Interrupt Controller) module which you can use to have up to 16 external interrupts on rising, falling or both edges.

External interrupts are only possible on the EXTINT0 to EXTINT15 pins (each of the EXTINTs is called an external interrupt line, e.g. EXTINT0 is called external interrupt line 0).

Posted by Uli Köhler in Electronics

## Where to find the SAMD20 microcontroller reference manual?

There is no such reference manual. All the information is included in the SAMD20 datasheet.

While you might be more familiar with microcontrollers such as the STM32 where there is a datasheet for each sub-family and a separate reference manual, Microchip/Atmel has chosen to include all the information in the datasheet.

Posted by Uli Köhler in Electronics

## Where to find the SAMD21 microcontroller reference manual?

There is no such reference manual. All the information is included in the SAMD21 datasheet.

While you might be more familiar with microcontrollers such as the STM32 where there is a datasheet for each sub-family and a separate reference manual, Microchip/Atmel has chosen to include all the information in the datasheet.

Posted by Uli Köhler in Electronics

## How to connect the WP (Write Protect) pin of an EEPROM?

Usually, you should connect the WP pin to GND. This disables write protection, i.e. you can read and write to the EEPROM.

The WP pin is active-high, i.e. pulling it to VCC enables the write protection. For details, refer to your EEPROM’s datasheet, e.g. the AT24CS04 dataheet. The function of the write protect pin is idential in all 24xx series EEPROMs, even across manufacturers.

In case you want to enable write protection to avoid accidentally overwriting your data, you should connect the WP pin to a microcontroller or an external logic circuitry. Connecting it to a microcontroller is typically the easiest option.

The following picture shows an EEPROM connected correctly in KiCAD so that read and write is enabled.

Posted by Uli Köhler in Electronics

## What is a SELV power supply?

SELV means Safety extra low voltage.

This means that

• the voltage at the output of the power supply is so low that is isn’t considered a safety risk (less than 60V DC or 35VAC).
• the secondary side is isolated from the primary side by double or reinforced insulation, so that the output terminals can’t become electrically dangerous by short-circuiting to the primary side (which is connected e.g. to 230VAC mains)
• the secondary side is isolated from earth so if there are faults in other devices connected to the same earth conductor, the output of the SELV power supply can’t get unsafe.

#### What does 60V DC mean in practice?

Typically the 60V value is defined as ripple-free DC. This means that the peak value of the waveform is not more than 10% higher than the maximum allowed voltage, e.g. it must not be more than 70V for a 60V system.

For 35VAC, the peak value has to be considered.

The values are typically measured across a 50kΩ resistance, refer e.g. to IEC61347-1 Annex A (the standard for safety of power supply for lighting purposes).

What does double or reinforced insulation mean?

This is an insulation that is so thick that even under extreme, abnormal conditions (like power surges due to lightning) it will not cause a discharge through the isolation.

This can be achieved by either using double insulation, which is just two layers of basic (non-reinforced) insulation, or using a much thicker type of insulation, reinforced isolation. In almost all cases, using reinforced isolation is more economical than using double insulation.

While this depends on the material, typically 0.4mm of insulating plastic foil is sufficient to count as reinforced insulation, however most standards require the insulator in use to be tested for its safety.

#### Can the primary side be connected to Earth?

Yes, the primary side may or may not be connected to Earth (as a third conductor). As long as the secondary side is not connected to earth, this does not affect the SELV rating, however note that this might have other implications since a power supply with primary connected to Earth is a Class I power supply whereas a power supply without any Earth connection is a Class II power supply.

There are many differences in how Class I and Class II power supplies are treated, so you need to check your applicable standards for details.

Posted by Uli Köhler in Compliance, Electronics

## What is a transformer construction according to clause 26.2.4.1 of IEC 61558-1?

In transformer specifications or tests, you will often find sentences like

Construction according to clause 26.2.4.1 of IEC 61558-1

This clause refers to hermetically sealed construction by impregnation potting (even if only parts of the transformer are potted), i.e. the transformer is (partially) filled with epoxy or similar potting materials in order to prevent moisture or dust from influencing its performance.

This usually means:

• the transformer can be smaller since the safety regulations (IEC61558-1) requires  less creepage distances compared to non-potted transformers
• the transformer manufacturer has to perform additional tests according to IEC61558-1 (compared to non-potted transformers) to prove that the potting material provides sufficient isolation
• the transformer is typically more expensive than an equivalent non-potted transformer since it needs to be potted in the factory and additional safety tests need to be performed on transformer specimens.
Posted by Uli Köhler in Compliance, Electronics

## What is a transformer construction according to clause 19.12.3 of IEC 61558-1?

In transformer specifications or tests, you will often find sentences like

Construction according to clause 19.12.3 of IEC 61558-1

This clause refers to isolated winding wire construction, i.e. a way of making a transformer where the isolation of the winding wires themselves is the only isolation.

This usually means:

• that the winding wires will have multiple layers of isolation (usually two or three to satisfy safety requirements from IEC61558-1)
• that there is no additional isolation foil since the isolation of the wound wires is sufficient to fulfil the safety requirements
• that manufacturer will need to perform additional safety tests for each and every transformer (100% production test) to prove that the transformer is safe.
Posted by Uli Köhler in Compliance, Electronics

## How to fix PyVISA “No module named ‘serial.tools'”

### Problem:

You want to use an ASRL (serial) instrument in PyVISA, but when you run

python3 -m visa info

you get this output even though you have serial installed:

ASRL INSTR:
Please install PySerial (>=3.0) to use this resource type.
No module named 'serial.tools'


### Solution:

You have installed serial but you need to install pyserial – they are not the same!

First you need to remove the system package python3-serial if installed. Example for Ubuntu/Debian:

sudo apt remove python3-serial

and also remove the pip serial package if installed

sudo pip3 uninstall serial

Then install pyserial:

sudo pip3 install pyserial

You can check if PySerial is installed properly using

python3 -m visa info

It should show you

ASRL INSTR: Available via PySerial (3.4)


once pyserial is installed correctly!

Note: The commands above are for Python 3.x. In case you are still using Python 2.x use pip2 or pip instead of pip3 and use python-serial instead of python3-serial as APT package name.

Posted by Uli Köhler in Electronics, Python

## How to fix PyVISA not finding any ASRL (serial port) instruments

### Problem:

You are trying to connect to a USB instrument using PyVISA & pyvisa-py, but the PyVISA resource manager doesn’t find any instruments:

#!/usr/bin/env python3
import visa
rm = visa.ResourceManager()
print(rm.list_resources()) # Prints "()" => No instruments found!

### Solution:

Install PySerial 3.0+:

First you need to remove the system package python3-serial if installed. Example for Ubuntu/Debian:

sudo apt remove python3-serial

and also remove the pip serial package if installed (we need to install pyserial, not serial!)

sudo pip3 uninstall serial

Then install pyserial:

sudo pip3 install pyserial

You can check if PySerial is installed properly using

python3 -m visa info

It should show you

ASRL INSTR: Available via PySerial (3.4)


if pyserial is installed correctly!

Note: The commands above are for Python 3.x. In case you are still using Python 2.x use pip2 or pip instead of pip3 and use python-serial instead of python3-serial as APT package name.

Posted by Uli Köhler in Electronics, Python

## How to fix PyVISA ‘Found a device whose serial number cannot be read. The partial VISA resource name is: USB0::[…]::[…]::???::0::INSTR’

### Problem:

You are trying to list available resources using PyVISA e.g. using

import visa
rm = visa.ResourceManager()
print(rm.list_resources())

But when you try to run it, you see an output like

Found a device whose serial number cannot be read. The partial VISA resource name is: USB0::6833::3601::???::0::INSTR
()

### Solution:

Even though PyVISA doesn’t tell you that exactly, this is just the bog-standard Linux USB permission problem. We already provided a generic solution in How to fix ALL USB permission issues on Linux once and for all.

Excerpt from this post (see there for details on why it works):

Run this in your favourite shell:

wget https://techoverflow.net/scripts/udev-install-usbusers.sh | sudo bash -s \$USER

This will print:

SUBSYSTEM=="usb", MODE="0666", GROUP="usbusers"
USB device configuration has been installed. Please log out and log back in or reboot

then log out and log back in (or close your SSH session and log back in).

In case this doesn’t work, reboot!

After that, your PyVISA script should work as intended and should print e.g.

('USB0::6833::3601::DL3A204800938::0::INSTR')

Posted by Uli Köhler in Electronics, Python

## How to fix PyVISA not finding any USB instruments

### Problem:

You are trying to connect to a USB instrument using PyVISA & pyvisa-py, but the PyVISA resource manager doesn’t find any instruments:

#!/usr/bin/env python3
import visa
rm = visa.ResourceManager()
print(rm.list_resources()) # Prints "()" => No instruments found!

### Solution:

In order for pyvisa-py to be able to connect to USB instruments, you need to install the Python usb library!

On Debian or Ubuntu, install it using

sudo apt-get -y install python3-usb

or, if you are still using Python 2.x

sudo apt-get -y install python-usb

Now, re-run the script – you should see an output like

('USB0::6833::3601::DL3A204800938::0::INSTR',)

In case you still don’t see the output, run python3 -m visa info or python -m visa info (for Python 2.x).

It should show an output like this:

Machine Details:
Platform ID:    Linux-4.19.0-5-686-i686-with-debian-10.0
Processor:

Python:
Implementation: CPython
Executable:     /usr/bin/python3
Version:        3.7.3
Compiler:       GCC 8.3.0
Bits:           32bit
Build:          Apr  3 2019 05:39:12 (#default)
Unicode:        UCS4

PyVISA Version: 1.9.1

Backends:
ni:
Version: 1.9.1 (bundled with PyVISA)
py:
Version: 0.3.1
ASRL INSTR: Available via PySerial (3.4)
USB INSTR: Available via PyUSB (1.0.2). Backend: libusb1
USB RAW: Available via PyUSB (1.0.2). Backend: libusb1
TCPIP INSTR: Available
TCPIP SOCKET: Available
GPIB INSTR:
Please install linux-gpib to use this resource type.
No module named 'gpib'


Check Backends -> py -> USB INSTR: If it’s not Available via PyUSB, check the information message for hints what might be the issue. For example, if it says

USB INSTR:
Please install PyUSB to use this resource type.
No module named 'usb'


that means that the Python USB library has not been installed properly.

If USB is Available via PyUSB but PyVISA still doesn’t find the instrument, check if it is connected properly using

lsusb

which should show a line related to your instrument’s manufacturer, e.g.

Bus 001 Device 002: ID 1ab1:0e11 Rigol Technologies

Also unplug and re-plug your instrument so Linux tries to reconnect to the USB device and check the end of the output of sudo dmesg which could list e.g.

[19427.230120] usb 1-2: new high-speed USB device number 2 using ehci-pci
[19427.425464] usb 1-2: config 1 interface 0 altsetting 0 bulk endpoint 0x82 has invalid maxpacket 64
[19427.425469] usb 1-2: config 1 interface 0 altsetting 0 bulk endpoint 0x3 has invalid maxpacket 64
[19427.425947] usb 1-2: New USB device found, idVendor=1ab1, idProduct=0e11, bcdDevice= 0.02
[19427.425950] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[19427.425953] usb 1-2: Product: DL3000 Serials
[19427.425955] usb 1-2: Manufacturer: Rigol Technologies.
[19427.425957] usb 1-2: SerialNumber: DL3A204800938
[19429.525745] usbcore: registered new interface driver usbtmc


usbtmc in the last line means that the USB device has been recognized as USB Test & Measurement class device, and hence you should be able to connect to it using PyVISA as USB INSTR.

Posted by Uli Köhler in Electronics, Python

## Capacitor energy from capacitance and voltage online calculator & Python code

C

V

#### Formula

$$E = \frac{1}{2}\cdot{}C\cdot{}U_p^2$$

#### Python code

You can use the UliEngineering library like this:

from UliEngineering.Electronics.Capacitors import capacitor_energy
from UliEngineering.EngineerIO import auto_format, auto_print

# These are equivalent:
energy = capacitor_energy("100 uF", "24 V") # energy = 0.0288 (J)
energy = capacitor_energy(100e-6, 24.0) # energy = 0.0288 (J)

# ... or get out a human-readable value:
energy_str = auto_format(capacitor_energy, "100 uF", "24 V") # "28.8 mJ"
# ... or print directly
auto_print(capacitor_energy, "100 uF", "24 V") # prints "28.8 mJ"


In case you can’t use UliEngineering, use this Python function:

def capacitor_energy(capacitance, voltage):
return 0.5*capacitance*voltage*voltage

# Usage example:
print(capacitor_energy(100e-6, 24.0)) # prints 0.0288 (J)

Posted by Uli Köhler in Calculators, Electronics

## X1/X2/Y1/Y2/Y4 impulse withstand rating voltage calculator (IEC 60384-14)

IEC 60384-14 specifies that X1/X2-rated capacitors shall be tested to withstand an impulse voltage of 4 kV (X1), 2.5 kV (X2, Y4), 8 kV (Y1) or 5 kV (Y2).

However these values only apply for a capacitance $\leq 1 μF$ (except for Y1/Y4 capacitors). Use this calculator for X1/X2/Y2 capacitances $> 1 μF$!

F

#### Formula:

$$Up = \frac{Up_{\leq 1 μF}}{\Large\sqrt{\frac{C}{1\,000\,000\frac{μF}{F}}}}$$

where:

• $Up$ is the impulse withstand voltage rating
• $C$ is the capacitance in Farads
• $Up_{\leq 1 μF}$ is the voltage rating for that capacitor class with a capacitance of $\leq 1 μF$:
• For X1-class: 4 kV
• For X2-class: 2.5 kV
• Y1-class impulse withstand voltage is always 8 kV no matter what capacitance
• For Y2-class: 5 kV
• Y4-class impulse withstand voltage is always 2.5 kV no matter what capacitance

#### Why is the impulse withstand voltage lower for larger capacitors?

The rationale behind the derating of the impulse withstand voltage is that larger capacitances will have sufficient capacitance so that a given overvoltage doesn’t cause a large voltage spike in the capacitor.

The formula (see above) is chosen so that the energy in the capacitor:

$$E = \frac{1}{2}\cdot{}C\cdot{}U_p^2$$

is kept constant (i.e. at the same value as for a equivalent capacitor of 1 μF).

Posted by Uli Köhler in Calculators, Compliance, Electronics

## What is performance criterion A/B/C/D in EMI tests?

When doing EMI immunity tests, you will often encounter specifications that your device should e.g. “meet Performance Criterion B during the ESD test”.

The performance criteria, as defined in the IEC 61000 series of standards, e.g. IEC 61000-4-5:2014 (surge test standard) are:

• Performance Criterion A: Your device continues to function normally during the test (i.e. no interruption)
• Performance Criterion B: Your device shows a malfunction but recovers automatically  (without user interaction) and then continues to operate normally.
• Performance Criterion C: Your devices shows a malfunction, but continues to operate normally only after user interaction (e.g. the user resets the device)
• Performance Criterion D: Your device has a permanent malfunction (e.g. some part of its hardware has been destroyed, or data being lost) which cannot be recovered from even after user interaction.

Additionally note that your device must not become dangerous under any circumstances, even if destroyed as per Performance Criterion D.

What defines a malfunction is up to the manufacturer of the device, but usually it’s related to whatever function it is advertised to perform. The manufacturer should define

#### Example: 230VLED bulb

• Performance Criterion A: LED Bulb continues to operate normally after the test
• Performance Criterion B: Bulb flickers during the test, but operates normally after the test
• Performance Criterion B: Bulb is less bright during the test but then operates normally.
• Performance Criterion B: Bulb blinks 10 times slowly but then operates normally
• Performance Criterion B: Bulb stays off for 20 seconds after the test but then goes back to normal.
• Performance Criterion C: Bulb goes out, but works normally after you switch off and on the power
• Performance Criterion C: Bulb continues to blink, but works normally after you switch off and on the power
• Performance Criterion D: Bulb goes out and does not turn on after you switch off and on the power
• Performance Criterion D: Bulb blinks after the test and continues to blink even if you switch off and on the power.

As a manufacturer you could define e.g.

• that the light intensity should be judged by eye (i.e. it should not be significantly dimmer after the test than a new bulb)
• that a luxmeter needs to be used to judge the light intensity after tests
• that the bulb may stay off for at most 15 seconds

and so on. In all but the most demanding applications, manufacturers define the criteria conservatively in order to increase the likelihood of passing the test. Since EMI laboratory tests are expensive, it’s in your best interest as a manufacturer not to over-define the criteria.

Posted by Uli Köhler in Electronics, EMI

## Inductive reactance online calculator & Python code

Use this online calculator to compute the reactance of an inductor in Ω at a specific frequency given its inductance.

H

Hz

#### Formula:

$$X_L = 2\pi fL$$

#### Python code:

The preferred way is to use UliEngineering’s UliEngineering.Electronics.Reactance.inductive_reactance:

from UliEngineering.Electronics.Reactance import *
# You can either pass strings like "150 uH" or values like 150e-6

inductive_reactance("150 uH", "10 MHz") # returns 9424.77796076938

from UliEngineering.Electronics.Reactance import *
from UliEngineering.EngineerIO import *

# Compute value as a string
xc = auto_format(inductive_reactance, "150 uH", "10 MHz") # "9.42 kΩ"

# ... or print directly
auto_print(inductive_reactance, "150 uH", "10 MHz") # prints "9.42 kΩ"

In case you can’t use UliEngineering and you want to do it manually, here’s a minimal example:

import math
def inductive_reactance(f, l):
"""Compute the inductive reactance"""
return 2*math.pi*f*l
Posted by Uli Köhler in Calculators, Electronics, Python

## Capacitive reactance online calculator & Python code

Use this online calculator to compute the reactance of a capacitor in Ω at a specific frequency given its capacitance.

F

Hz

#### Formula:

$$X_C = \frac{1}{2\pi fC}$$

#### Python code:

The preferred way is to use UliEngineering’s UliEngineering.Electronics.Reactance.capacitive_reactance:

from UliEngineering.Electronics.Reactance import *
# You can either pass strings like "150 pF" or values like 150e-12

capacitive_reactance("150 pF", "10 MHz") # returns 106.1032953945969

from UliEngineering.Electronics.Reactance import *
from UliEngineering.EngineerIO import *

# Compute value as a string
xc = auto_format(capacitive_reactance, "150 pF", "10 MHz") # "106 Ω"

# ... or print directly
auto_print(capacitive_reactance, "150 pF", "10 MHz") # prints "106 Ω"

In case you can’t use UliEngineering and you want to do it manually, here’s a minimal example:

import math
def capacitive_reactance(f, c):
"""Compute the capacitive reactance"""
return 1./(2*math.pi*f*c)
Posted by Uli Köhler in Calculators, Electronics, Python

## Volts to dBµV online calculator & Python code

Use this online calculator to convert a voltage in Volts to a voltage in dBµV.

V

#### Formula:

$$U_{\text{dBµV}} = \frac{20\cdot\log(1\,000\,000 \cdot U_V)}{ \log(2) + \log(5)}$$

#### Python code:

import math
def volts_to_dbuv(v):
"""Convert a voltage in volts to a voltage in dBµV"""
return (20*math.log(1e6 * v))/(math.log(2) + math.log(5))

Posted by Uli Köhler in Calculators, Electronics

## dBµV to Volts online calculator & Python code

Use this online calculator to convert a voltage in dBµV to a voltage in Volts.

dBµV

#### Formula:

$$U_{\text{Volts}} = \frac{10^{\frac{U_{dBµV}}{20}}}{1\,000\,000 \frac{V}{µV}}$$

#### Python code:

def dbuv_to_volts(dbuv):
"""Convert a voltage in dBµV to a voltage in volts"""
return (10**(dbuv/20.))/1e6

Posted by Uli Köhler in Calculators, Electronics

## How to install picamraw using pip

First try installing it normally:

sudo pip3 install picamraw

In case that fails with this error message (like for me):

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting picamraw
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://www.piwheels.org/simple/picamraw/

download it and install it manually: Copy the link of the most recent .whl file from https://pypi.org/project/picamraw/#files, download it using wget and install it using pip3, e.g.:

wget https://files.pythonhosted.org/packages/1e/47/4efb0d0ab5d40142424e7f3db545e276733a45bd7f7f9095919ef30c96b3/picamraw-1.2.64-py3-none-any.whl
sudo pip3 install picamraw-1.2.64-py3-none-any.whl

Posted by Uli Köhler in Python, Raspberry Pi