# Electronics

## How to fix Arduino ‘U8glib.h: No such file or directory’

### Problem:

You want to compile an Arduino sketch, but you see an error message like

In file included from sketch/ultralcd.cpp:96:0:
ultralcd_impl_DOGM.h:46:20: error: U8glib.h: No such file or directory
compilation terminated.
exit status 1
U8glib.h: No such file or directory

### Solution:

In Arduino, click on the Tools menu, then click on Manage libraries. Now enter u8glib into the search bar an press Enter.

Scroll down to the U8glib library, hover over it with your mouse and click Install on the bottom right. Now the error should be gone.

Posted by Uli Köhler in Arduino

## What pin is the green LED on the STM8S-Discovery connected to?

The LED is connected to pin PD0 on the STM8S105C6T6 on the STM8S-DISCOVERY.

Source: STM8S-DISCOVERY User manual

Posted by Uli Köhler in Embedded

## How to fix SDCC ‘at 1: warning 119: don’t know what to do with file ‘main.o’. file extension unsupported’

If you use SDCC to compile your C code for your microcontroller and you encounter an error message like this:

at 1: warning 119: don't know what to do with file 'main.o'. file extension unsupported

you have to configure your build system to use the .rel suffix for object files instead of the standard .o. SDCC expects built object files to have the .rel extension! See How to change CMake object file suffix from default “.o” for details on how to do that in CMake.

Posted by Uli Köhler in Build systems, Embedded

## A working SDCC STM8 CMake configuration

If you have been looking desperately for a working CMake example for the SDCC compiler for STM8 microcontrollers here’s my take on it:

cmake_minimum_required(VERSION 3.2)

set(CMAKE_C_OUTPUT_EXTENSION ".rel")
set(CMAKE_C_COMPILER sdcc)
set(CMAKE_SYSTEM_NAME Generic) # No linux target etc

# Prevent default configuration
set(CMAKE_C_FLAGS_INIT "")

SET(CMAKE_C_FLAGS "-mstm8 --std-c99")

# Flash targets


This will build main.ihx from main.c. main.ihx is a Intel Hex file which can be directly flashed using stm8flash.

The last lines setup make flash ; you might need to use the correct microcontroller (stm8s105c6 in this example, run stm8flash -l to show supported devices) and the correct flash adapter (stlink, stlinkv2, stlinkv21, stlinkv3 or espstlink).

The setup example shown here is for the STM8S eval board.

I suppose it can be easily modified for other microcontrollers, but I haven’t tried that so far.

Posted by Uli Köhler in CMake, Embedded, Hardware

## What’s the formula for 2/3/4 parallel resistors?

#### Formula for 2 parallel resistors R1 and R2:

$$R_{total} = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2}}$$

Python code:

rtotal = 1. / (1./r1 + 1./r2)

#### Formula for 3 parallel resistors R1 and R2:

$$R_{total} = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2} + \frac{1}{R_3}}$$

Python code:

rtotal = 1. / (1./r1 + 1./r2 + 1./r3)

#### Formula for 4 parallel resistors R1 and R2:

$$R_{total} = \frac{1}{\frac{1}{R_1} + \frac{1}{R_2} + \frac{1}{R_3} + \frac{1}{R_4}}$$

Python code:

rtotal = 1. / (1./r1 + 1./r2 + 1./r3 + 1./r4)

Posted by Uli Köhler in Electronics, Python

## Computing crystal load capacitance using Python & UliEngineering

When you implement a crystal oscillator circuit, one task that is both essential and often overlooked is how to compute the load capacitors of the crystal.

Tip: In many cases it is easier to use an integrated crystal oscillator for which you don’t have to care about load capacitors. Crystals are usually recommendable for low-power applications and high-volume manufacturing where oscillators are too expensive.

In this example, we’ll compute the load capacitors for a Abracon ABLS-16.000MHZ-B4-T (specified with a load capacitance of 18 pF according to the datasheet).

It is paramount to understand that 18 pF load capacitance specification does not mean that you can use 18 pF capacitors or 2x 9 pF capacitors. You need to actually calculate the correct values.

You need the following information:

• Load capacitance of the crystal, specified in the crystal’s datasheet.
• An estimate of the stray capacitance, i.e. the capacitance of the PCB traces from the crystal to the microcontroller. If you don’t know this, I recommend to use 2 pF
• The pin capacitance of the microcontroller (or whatever device you want to connect your device to) as specified in the microcontroller’s datasheet. Often the capacitance of oscillator pins is not the same as for other pins and is hence specified separately. 3 pF is a good first estimate for most modern ICs. The value must be specified per pin!

In order to install UliEngineering (a Python 3 library) run:

sudo pip3 install -U UliEngineering

Now we can compute the correct load capacitors using:

from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Crystal import *

# Print load capacitors: prints "29.0 pF"
auto_print(load_capacitors, cload="18 pF", cpin="3 pF", cstray="2 pF")

In this case, you need to have two load capacitors of 29.0 pF each.

You should use the closest available value of load capacitor, but always check the resulting frequency if you have high clock accuracy requirements.

Tip: You can pass both numbers (like 18e-12) or strings (like 18 pF or 0.018 nF) to most UliEngineering functions. SI prefixes like p and n are automatically decoded

If you want to use the value programmatically, call load_capacitors() directly:

from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Crystal import *

# Coompute the load capacitor value (for both of the two load caps):
# load_caps = 2.9e-11

Posted by Uli Köhler in Electronics, Python

## Computing resistor power dissipation in Python using UliEngineering

In this example we’ll calculate the power dissipation of a 1 kΩ resistor with a constant current of 30 mA flowing through it, using our science and engineering Python library UliEngineering.

In order to install UliEngineering (a Python3-only library), run

sudo pip3 install -U UliEngineering

Now we can compute the resistor power dissipation using power_dissipated_in_resistor_by_current()

from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Resistors import *
# Just compute the value:
power = power_dissipated_in_resistor_by_current("1 kΩ", "30 mA") # power = 0.9

# Print value: prints: prints "900 mW"
auto_print(power_dissipated_in_resistor_by_current, "1 kΩ", "30 mA")

Since the result is 900 mW, you can deduce that you need to use a resistor with a power rating of at least one Watt.

Note that you can pass both numbers (like 0.03) or strings (like 30 mA or 0.03 A) to most UliEngineering functions. SI prefixes like k and M are automatically decoded

If you know the voltage across the resistor, you can use power_dissipated_in_resistor_by_voltage(). Let’s assume there is 1V dropped across the resistor:

from UliEngineering.EngineerIO import auto_print
from UliEngineering.Electronics.Resistors import *
# Just compute the value:
power = power_dissipated_in_resistor_by_voltage("1 kΩ", "30 mA") # power = 0.001

# Print value: prints: prints "1.00 mW"
auto_print(power_dissipated_in_resistor_by_voltage, "1 kΩ", "30 mA")

So in this case the power dissipation is extremely low – only 1.00 mW – and won’t matter for most practical applications.

In many cases, you can also pass NumPy arrays to UliEngineering functions:

from UliEngineering.EngineerIO import format_value
from UliEngineering.Electronics.Resistors import *
import numpy as np

# Compute the value:
resistors = np.asarray([100, 500, 1000]) # 100 Ω, 500 Ω, 1 kΩ
power = power_dissipated_in_resistor_by_voltage(resistors, "30 mA") # power = 0.001

# power = [9.0e-06 1.8e-06 9.0e-07]

Posted by Uli Köhler in Electronics, Python

## How to fix raspivid ‘* failed to open vchiq instance’

### Problem:

You want to capture a video using raspivid, e.g. using a command like

raspivid -o vid.h264

but you only see this error message:

* failed to open vchiq instance

with no video being captured.

### Quick solution:

Run raspivid using sudo:

sudo raspivid -o vid.h264

Now either the video will be captured or you will see another error message if there is another issue with your camera setup.

### Better solution:

Add your user to the video group so you don’t have to run raspivid as root:

sudo usermod -a -G video $USER You only need to do this once. After that, log out and log back in (or close your SSH session and connect again). If in doubt, restart the Raspberry Pi. See What does &#8217;sudo usermod -a -G group$USER&#8216; do on Linux? for more details.

Now you will be able to capture video as normal user:

raspivid -o vid.h264

Posted by Uli Köhler in Embedded, Linux

## How to enable SSH on Raspbian without a screen

You can open the boot partition on the SD card (the FAT32 partition) and create an empty file named ssh in the root directory of that partition. Ensure that the file is names ssh and not ssh.txt !

If you are in the correct working directory in the command line, use

touch ssh

On recent Ubuntu version, this will switch to the correct directory and create the file (but you need to mount the directory manually e.g. using your file explorer:

cd /media/$USER/boot && touch ssh Don’t forget to unmount the boot drive before removing the SD card. Once you restart the Raspberry Pi with the modified SD card, SSH will be enabled without you having to attach a keyoard or screen to the Pi. This approach was tested with the 2018-11-13 version of Raspbian and works with Raspberry Pi 1, Raspberry Pi 2 and Raspberry Pi 3. Credits to Yahor for the original solution on StackOverflow! Posted by Uli Köhler in Embedded, Linux ## Resistor power dissipation calculator Use this online calculator to calculate the power dissipation in a purely resistive load. Continue reading → Posted by Uli Köhler in Calculators, Electronics ## What is the default SSH username/password for OctoPrint/OctoPi? OctoPrint/OctoPi uses the standard Raspbian credentials, that is: Username: pi Password: raspberry Tested with OctoPrint 0.16.0 Posted by Uli Köhler in Embedded, Linux ## How to configure OctoPrint/OctoPi with Ethernet using a static IP In order to configure OctoPrint/OctoPi to use the Raspberry Pi Ethernet interface with a static IP, first open the rootfs partition on the SD card. After that, open etc/network/interfaces in your preferred text editor (you might need to open it as root, e.g. sudo nano etc/network/interfaces – ensure that you don’t edit your local computer’s /etc/network/interfaces but the one on the SD card). Now copy the following text to the end of etc/network/interfaces: auto eth0 allow-hotplug eth0 iface eth0 inet static address 192.168.1.234 netmask 255.255.255.0 gateway 192.168.1.1 network 192.168.1.0 broadcast 192.168.0.255 dns-nameservers 8.8.8.8 8.8.4.4 You might need to adjust the IP addresses so they match your router. Save the file and insert the SD card into your Raspberry Pi. You should be able to ping in – in our example, ping 192.168.1.234. Tested with OctoPrint 0.16.0 Original source: OctoPrint forum Posted by Uli Köhler in Embedded, Linux ## How to fix MicroPython I2C no data ### Problem: You’ve configured MicroPython’s I2C similar to this (in my case on the ESP8266 but this applies to many MCUs): i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4)) but you can’t find any devices on the bus: >>> i2c.scan() [] ### Solution: Likely you forgot to configure the pins as pullups. I2C needs pullups to work, and many MCUs (like the ESP8266) provide support for integrated (weak) pull-ups. p4 = machine.Pin(4, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP) p5 = machine.Pin(5, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP) i2c = machine.I2C(-1, p5, p4) i2c.scan() # [47] You can also verify this by checking with a multimeter or an oscilloscope: When no communication is going on on the I2C bus, the voltage should be equivalent to the supply voltage of your MCU (usually 3.3V or 5V – 0V indicates a missing pullup or some other error). Posted by Uli Köhler in Electronics, MicroPython, Python ## How to fix MicroPython ‘ValueError: invalid I2C peripheral’ If you see the error message Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid I2C peripheral you are likely running code like this: import machine i2c = machine.I2C(machine.Pin(5), machine.Pin(4)) #### Solution The MicroPython API has changed (source: forum). You need to use this syntax instead: import machine i2c = machine.I2C(-1, machine.Pin(5), machine.Pin(4)) -1 is the I2C ID that selects a specific peripheral. -1 selects a software I2C implementation which can work on most pins. See the MicroPython I2C class documentation for more details. Posted by Uli Köhler in Electronics, MicroPython, Python ## How to calculate STM32 bxCAN bit timings using Python When I needed to configure some STM32 microcontrollers with nonstandard clock speeds to use the correct CAN bit timings. While this script has been written for the STM32F413 series, it should be applicable to pretty much all STM32 MCUs with bxCAN – however, you need to check the details, especially from which clock the CAN is derived. You need to know: • The frequency of the clock from which the CAN clock is derived (PCLK1 in my example: 48 MHz) • Your desired Baudrate: 1 MBaud in my example Using an iterative trial and error approach, we will determine the appropriate values for BRP, TS1 and TS2 (these are specific sets of bits in the CAN): #!/usr/bin/env python3 # Configuration. Change here master_clock = 48e6 # PCLK1, or whatever CLK CAN is derived from desired_baudrate = 1e6 # 1e6 = 1 MBaud, 500e3 = 500 kBaud # Register values brp = 5 # Baud rate prescaler ts1 = 4 # (Length of time segment 1) - 1 ts2 = 1 # (Length of time segment 2) - 1 # Calculation. You usually don't need to change this. bs1_segments = ts1 +1 bs2_segments = ts2 + 1 total_segments = 1 + bs1_segments + bs2_segments bittime = 1. / desired_baudrate master_time = 1. / master_clock tq = (brp + 1) * master_time total_time = total_segments * tq effective_rate = 1. / total_time sample_point = 1 - (bs2_segments / total_segments) # Print results print(f"Effective Baudrate: {effective_rate:.2f} Baud = {100. * effective_rate / desired_baudrate:.2f} % of desired baudrate") print(f"Sample point: {100. * sample_point:.2f} %")  When you run this script, you will see an output like this: Effective Baudrate: 1000000.00 Baud = 100.00 % of desired baudrate Sample point: 75.00 % Of course you need to fill the master clock frequency and the desired baudrate at the top. You can start with the pre-configured values for BRP, TS1 and TS2. In the end, you should achieve 100% of desired baudrate and a sample point that is 87.5% (80% to 90% is usually OK, 75% might not work with long cables. There might be a specific value dictated by your comms standard, e.g. CANOpen or J1939). Change the values of for BRP, TS1 and TS2., re-running the script and observing the output each time. I recommend first changing BRP until you get close and then adjusting using this algorithm: • If your actual baudrate is too slow (i.e. < 100%), decrease BRP (larger step) and/or decrease TS1 + TS2 • If your actual baudrate is too fast (i.e. > 100%), increase BRP (larger step) and/or increase TS1 + TS2 • If your sample point percentage is too small, increase the ratio of TS1 to TS2 • If your sample point percentage is too large, decrease the ratio of TS1 to TS2 Note that there might be master clock speeds and baudrates for which there is no ideal setting. Be sure to check whether a slightly different baudrate is still OK in your application (usually it’s not). If it is not, you need to find a way to use a different PCLK1 speed. Posted by Uli Köhler in Electronics, Embedded ## Fixing ScanaStudio error while loading shared libraries: libftd2xx.so: cannot open shared object file on Ubuntu ### Problem: You want to start IkaLogic ScanaStudio, but you see the following error message: ./ScanaStudio: error while loading shared libraries: libftd2xx.so: cannot open shared object file: No such file or directory ### Solution: You need to download the D2XX drivers from the FTDI D2XX drivers page. Look for your architecture in the columns (usually x64 (64 bit), this is the same as x86_64) and click the link in the Linux row. This will download a file named something like libftd2xx-x86_64-1.4.8.gz. Now you can extract the archive using tar xvf libftd2xx-* Now we can copy the shared object file to the IkaLogic ScanaStudio directory: cp release/build/libftd2xx.so.* ~/Ikalogic/ScanaStudio/libftd2xx.so Now you can start ScanaStudio: cd ~/Ikalogic/ScanaStudio ./ScanaStudio Posted by Uli Köhler in Electronics, Linux ## How to fix Arduino SigFox.h: No such file or directory ### Problem: You are trying to run the Arduino SigFox first configuration tutorial but when trying to upload the sketch, you get this error message: SigFox.h: No such file or directory ### Solution: While you have installed the board libraries and compiler for the SigFox boards, you have not installed the SigFox library itself. Press Ctrl+Shift+I to open the library manager (you can also open it from the Tools menu). Look for Arduino SigFox for MKRFox1200 by Arduino and click the Install button Since the SigFox sketches also require the Arduino low power libraries (i.e. you would get the error message ArduinoLowPower.h: No such file or directory), also click the Install button for the Arduino Low Power by Arduino library. After that, enter RTCZero in the search bar at the top of the library manager and click the Install button for the RTCZero by Arduino library. After that, retry uploading the sketch. Posted by Uli Köhler in Arduino, Electronics, Embedded ## Identifying the frame length for an unknown serial protocol Let’s suppose you’re reverse-engineering a serial protocol. You already know the correct configuration for the serial port (baudrate etc.) and you assume the protocol is built from frames of equal length. For simplicity we will also assume that the device sends the data without the necessity to request data from it first. If this is not the case, you can use a different and much simpler approach (just send the request character and se The next step is to determine the the frame length of the protocol. This post not only details two variants of one of the algorithms you can use in order to do this, but also provides a ready-to-use Python script you can use for your own protocols. ## Approach 1: Autocorrelation with argmax We will use a simple mathematical approach in order to find out what the most likely frame length will be. This is based on the assumption that frames will have a high degree of self similarity, i.e. many of the bytes in a single frame will match the corresponding bytes in the next frame. It is not required that all bytes are the same in every frame, but if you have entirely different bytes in every frame, the approach will likely not deduce the correct frame length. This approach is based on autocorrelation. Although it sounds complicated, it means nothing more Compare a sequence by a delayed/shifted version of itself. This means we will perform the following steps: • Read a set of characters from the serial device • Correlate the set of characters with shifted versions of itself • The framelength is the shift where the maximum similarity occurs (using np.argmax) As similiarity score, we’ll use 1 if the bytes equal or 0 else. For specific protocols, it might be a more viable approach to introduce individual bit matching, but this will also introduce noise into the process. For most simple protocols I’ve seen, this approach works very well for both ASCII and binary. Plotting the correlation looks like this: ## Approach 2: Multiple-shift aware Autocorrelation This modified algorithms works well for protocols where there is insignificant similarity between any two frames or if there is a lot of noise. For such protocol, the maximum score approach does not yield the correct result. However, we can use the property of constant-framelength protocols that we get high matching scores by shifting a frame by an integer multiple of the (unknown) framelength. Instead of just taking one maximum peak, we multiply all the scores for the integer-multiples of any length. While this approach doesn’t sound too complicated compared to the first one, it has more caveats and pitfalls, e.g. that there are no integer multiples within the data array for the second half of the correlation result array, and the second quarter is not very significant as there are not many multiples to multiply. The script (see below) works around these issues by only computing the first quarter of the possible result space. Use the -n parameter in order to increase the number of characters read by the script. After computing the multiple-shift aware correlation, we can use argmax just like in the first approach to find the best correlation. Sometimes this identifies a multiple of the frame length due to noise. You can look at the plot (use -p) and manually determine the frame length in order to find the correct frame length. As you can see from the result, the “noise” (in between the frame-length shift matches, caused by random matches between characters) is mostly gone. In many real usecases, this algorithm will produce a more distinct signal in the plot, but the automatically calculated frame size will not be correct as several effects tend to increase the lobe height for multiples of the frame height. Therefore, it is adviseable to have a look at the plot (-p in the script) before taking the result as granted. ## Automating the algorithm Here’s the Python3 script to this article which works well without modification, but for some protocols you might need to adjust it to fit your needs: #!/usr/bin/env python3 """ ProtocolFrameLength.py Determine the frame length of an unknown serial protocol with constant-length frames containing similar bytes in every frame. For an explanation, see Identifying the frame length for an unknown serial protocol Example usage:$ python3 ProtocolFrameLength.py -b 115200 /dev/ttyACM0
"""
import serial
import numpy as np
import math
from functools import reduce
import operator

__author__ = "Uli Köhler"
__version__ = "1.0"
__email__ = "[email protected]"

def match_score(c1, c2):
"""
Correlation score for two characters, c1 and c2.
Uses simple binary score
"""
if c1 is None or c2 is None:  # Fill chars
return 0
return 1 if c1 == c2 else 0

def string_match_score(s1, s2):
assert len(s1) == len(s2)
ln = len(s1)
return sum(match_score(s1[i], s2[i]) for i in range(ln))

def compute_correlation_scores(chars, nomit=-1):
# Omit the last nomit characters as single-char matches would be over-valued
if nomit == -1: # Auto-value
nomit = len(chars) // 10
corr = np.zeros(len(chars) - nomit)
# Note: autocorrelation for zero shift is always 1, left out intentionally!
for i in range(1, corr.size):
# build prefix by Nones
prefix = [None] * i
s2 = prefix + list(chars[:-i])
# Normalize by max score attainable due to Nones and the sequence length (there are i Nones)
corr[i] = string_match_score(chars, s2) / (len(chars) - i)
return corr

def print_most_likely_frame_length(correlations):
# Find the largest correlation coefficient. This model does not require a threshold
idx = np.argmax(correlations)
print("Frame length is likely {} bytes".format(idx))

def plot_correlations(correlations):
from matplotlib import pyplot as plt
plt.style.use("ggplot")

plt.title("Correlation scores for a protocol with 16-byte frames")
plt.gcf().set_size_inches(20,10)
plt.plot(correlations)

plt.title("Correlation scores")
plt.ylabel("Normalized correlation score")
plt.xlabel("Shift")

plt.show()

"""
Multi-shift aware algorithm
"""
corr_multishift = np.zeros(correlations.size // 4)
for i in range(1, corr_multishift.size):
# Iterate multiples of i (including i itself)
corr_multishift[i] = reduce(operator.mul,
(correlations[j] for j in range(i, correlations.size, i)), 1)
return corr_multishift

if __name__ == "__main__":

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('port', help='The serial port to use')
parser.add_argument('-b', '--baudrate', type=int, default=9600, help='The baudrate to use')
parser.add_argument('-m', '--multishift', action="store_true", help='Use the multi-shift aware autocorrelation algorithm')
parser.add_argument('-p', '--plot', action="store_true", help='Plot the resulting correlation matrix')
args = parser.parse_args()

ser = serial.Serial(args.port, args.baudrate)
ser.reset_input_buffer()

corr = compute_correlation_scores(chars)
if args.multishift:
print_most_likely_frame_length(corr)

if args.plot:
plot_correlations(corr)


Usage example:

python3 ProtocolFrameLength.py -b 115200 /dev/ttyACM0

You can use -m to use the multiple-shift aware approach.

Use -p to plot the results as shown above. If one of the approaches does not work, it is advisable to plot the result in order to see if there is something visible in the plot which has not been detected by the

As the results depend on the actual data read, it is advisable to perform multiple runs and see if the results vary.

Posted by Uli Köhler in Electronics, Embedded

## Accurate calculation of PT100/PT1000 temperature from resistance

PT100/PT1000 temperatures calculation suffers from accuracy issues for large sub-zero temperatures. UliEngineering implements a polynomial-fit based algorithm to provide $58.6 \mu{\degree}C$ peak-error over the full defined temperature range from -200 {\degree}C to +850 °C.

Use this code snippet (replace pt1000_ by pt100- to use PT100 coefficients) to compute an accurate temperature (in degrees celsius) e.g. for a resistane of 829.91 Ω of a PT1000 sensor.

from UliEngineering.Physics.RTD import pt1000_temperature
# The following calls are equivalent and print -43.2316359463
print(pt1000_temperature("829.91 Ω"))
print(pt1000_temperature(829.91))


You install the library (compatible to Python 3.2+) using

\$ pip3 install -U UliEngineering
Posted by Uli Köhler in Electronics, Mathematics

## Fixing ssh: Exited: String too long on OpenWRT

### Problem

When trying to execute SSH on OpenWRT with a private key, e.g.

ssh -i id_rsa [email protected]

you encounter this error:

ssh: Exited: String too long
Posted by Uli Köhler in Embedded, Linux