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.

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

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.

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

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)
      Binary library: Not found
   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: 230V LED 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.

Also see Capacitive reactance online calculator & Python code

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

Or get a human-readable value:

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.

Also see Inductive reactance online calculator & Python code

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

Or get a human-readable value:

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.

Also see dBµV to Volts online calculator & Python code

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.

Also see Volts to dBµV online calculator & Python code

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

How to capture Raspi Camera image using OpenCV & Python

First, install OpenCV for Python 3:

sudo apt install python3-opencv

Here’s the code to acquire the image and store it in image.png:

#!/usr/bin/env python3
import cv2
video_capture = cv2.VideoCapture(0)
# Check success
if not video_capture.isOpened():
    raise Exception("Could not open video device")
# Read picture. ret === True on success
ret, frame = video_capture.read()

cv2.imwrite('image.png', frame)
# Close device
video_capture.release()

Run it using

python3 cv-raspicapture.py

 

Posted by Uli Köhler in OpenCV, Python, Raspberry Pi

How to fix Raspi camera ‘mmal_vc_component_create: failed to create component ‘vc.ril.camera’ (1:ENOMEM)’

Problem:

You are trying to acess the Raspberry Pi camera using raspistill or raspivid, but you see an error message like this:

mmal: Cannot read camera info, keeping the defaults for OV5647
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: Failed to create camera component
mmal: main: Failed to create camera component
mmal: Camera is not enabled in this build. Try running "sudo raspi-config" and ensure that "camera" has been enabled

Solution:

The most common issue here is that you don’t have the camera interface enabled. Read How to enable Raspberry Pi camera using raspi-config for instructions on how to do that.

If the camera interface has already been enabled (remember that you need to reboot for the changes to take effect), the most likely reason for error messages like this is that the camera is not connected correctly to the Raspberry Pi:

  • Is the CSI cable inserted the right way? The silvery contacts need to face away from the Ethernet connector!
  • Is the CSI cable fully seated?
  • Did you insert the CSI cable into the Display connector? It needs to be inserted into the CSI connector, which is the one closer to the Ethernet connector.
  • Is the other end of the CSI cable correctly attached to the camera board?
Posted by Uli Köhler in Embedded, Raspberry Pi

How to enable Raspberry Pi camera using raspi-config

You can enable the camera interface by running

sudo raspi-config

Select 5 Interfacing Options and press Return.

Now select P1 Camera and press Return.

Select Yes and press Return.

Now that the camera interface has been enabled, press Return.

Press Tab twice to select Finish and press Return. You are now asked if you want to reboot:

Select Yes and wait for your Raspberry Pi to reboot. Your camera interface will be enabled after the reboot.

In case you are not asked to reboot, your camera interface was already enabled. In this case, select Finish on the main raspi-config screen by pressing Tab twice and pressing Return:

After pressing Return, check your shell again. Reboot (sudo reboot) so that any previous settings will take effect and then try

sudo raspistill -o myimg.jpg

in order to test your camera.

Posted by Uli Köhler in Embedded, Raspberry Pi

How to fix Raspi camera ‘failed to open vchiq instance’

Problem:

You are trying to access the Raspberry Pi camera using raspistill or raspivid. Although you already enabled the camera interface using raspi-config, you see this error message:

* failed to open vchiq instance

Solution:

Your user does not have the permissions to access the camera interface. As a quick fix, you can run raspistill or raspivid as root using sudo, or add your user to the video group to acquire the required permissions:

usermod -a -G video $USER

After doing this, log out and log back in again for the changes to take effect – or close your SSH session and connect again. If in doubt, try to reboot.

Posted by Uli Köhler in Embedded, Raspberry Pi