# Trinkey True Random Number Generator

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/107/232/medium800/sensors_banner.jpg?1639174594)

Random numbers have a lot of uses in computer programming. These can be for fun things like picking a random song, drawing to a random location, etc. On the more serious side, they are useful in the realm of cryptography. For the later, security is a primary concern, so the actual randomness of the random number becomes increasingly important. This is where using a [True Random Number Generator (TRNG)](https://en.wikipedia.org/wiki/Hardware_random_number_generator) is very beneficial.

Don't have a TRNG? No worries. In this guide we'll show how to easily create a TRNG by coupling an [Adafruit Trinkey QT2040](https://www.adafruit.com/product/5056) with an [Infineon Trust M breakout](https://www.adafruit.com/product/4351). The two boards can be mounted together using M2.5 screws ([kit here](https://www.adafruit.com/product/5248)) or similar. Further, by connecting the boards using the STEMMA QT connector and [cable](https://www.adafruit.com/product/4399) - **no soldering is required**. Simply connect everything, load the provided code, plug into your PC's USB port, and connect to the serial port to receive all the true random goodness you may need.

## Parts

Here is a list of the hardware items used in this project:

### Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT

[Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT](https://www.adafruit.com/product/5056)
It's half USB Key, half Adafruit QT Py, and a lotta&nbsp;RP2040_..._it's&nbsp; **Trinkey QT2040** , the circuit board with an RP2040 heart and Stemma QT legs. Folks are loving the [QT Py 2040](https://www.adafruit.com/product/4900) we made, but maybe you...

In Stock
[Buy Now](https://www.adafruit.com/product/5056)
[Related Guides to the Product](https://learn.adafruit.com/products/5056/guides)
![Video of Trinkey RP2040 plugged into a laptop. An OLED display is connected and shows a graphic keyboard cat animation.](https://cdn-shop.adafruit.com/product-videos/640x480/5056-04.jpg)

### Adafruit Infineon Trust M Breakout Board - STEMMA QT / Qwiic

[Adafruit Infineon Trust M Breakout Board - STEMMA QT / Qwiic](https://www.adafruit.com/product/4351)
This is a STEMMA I2C&nbsp;breakout for the [Infineon OPTIGA TRUST M SLS 32AIA](https://www.infineon.com/cms/en/product/security-smart-card-solutions/optiga-embedded-security-solutions/optiga-trust/optiga-trust-m-sls32aia/).

OPTIGA Trust M is the next generation of Trust X....

In Stock
[Buy Now](https://www.adafruit.com/product/4351)
[Related Guides to the Product](https://learn.adafruit.com/products/4351/guides)
![Overhead shot of black, rectangular security breakout board.](https://cdn-shop.adafruit.com/640x480/4351-09.jpg)

### Stacking M2.5 Hardware Kit for STEMMA QT and RP2040 Trinkey

[Stacking M2.5 Hardware Kit for STEMMA QT and RP2040 Trinkey](https://www.adafruit.com/product/5248)
&nbsp;

Here is the perfect hardware kit to make a [RP2040 QT Trinkey](https://www.adafruit.com/product/5056) into any kind of USB-connected smart sensor with a QT-sensor-sandwich!&nbsp;You can assemble it with just a plain Phillips screwdriver. Takes less than a minute to bolt...

In Stock
[Buy Now](https://www.adafruit.com/product/5248)
[Related Guides to the Product](https://learn.adafruit.com/products/5248/guides)
![Angled shot of a USB key-shaped PCB with a sensor board stacked on top via black nylon screws.](https://cdn-shop.adafruit.com/640x480/5248-00.jpg)

### STEMMA QT / Qwiic JST SH 4-Pin Cable - 50mm Long

[STEMMA QT / Qwiic JST SH 4-Pin Cable - 50mm Long](https://www.adafruit.com/product/4399)
This 4-wire cable is&nbsp;50mm / 1.9" long and fitted with JST SH female 4-pin connectors on both ends. Compared with the chunkier JST PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and remove.

<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4399)
[Related Guides to the Product](https://learn.adafruit.com/products/4399/guides)
![Angled of of JST SH 4-Pin Cable.](https://cdn-shop.adafruit.com/640x480/4399-00.jpg)

# Trinkey True Random Number Generator

## Assembly

Using the [Stacking M2.5 Hardware Kit](https://www.adafruit.com/product/5248) and a [STEMMA QT cable](https://www.adafruit.com/product/4399), assembling everything together can be done by hand. **No soldering required.**

Gather all the parts.

![sensors_assy1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/107/136/medium640/sensors_assy1.jpg?1638986024)

Use the screws to attach the hex standoffs to the Trinkey QT2040.

![sensors_assy2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/107/137/medium640/sensors_assy2.jpg?1638986062)

Flip over and mount the Trust M breakout on the hex standoffs.

![sensors_assy3.jpg](https://cdn-learn.adafruit.com/assets/assets/000/107/138/medium640/sensors_assy3.jpg?1638986132)

Screw the hex nuts on to the hex standoffs to hold the Trust M breakout in place.

![sensors_assy4.jpg](https://cdn-learn.adafruit.com/assets/assets/000/107/140/medium640/sensors_assy4.jpg?1638986180)

Use the STEMMA QT cable to connect the Trust M breakout to the Trinkey QT2040.

![sensors_assy5.jpg](https://cdn-learn.adafruit.com/assets/assets/000/107/142/medium640/sensors_assy5.jpg?1638986237)

# Trinkey True Random Number Generator

## Sending TRNG

The idea for the TRNG sending code is very simple:

1. Get TRNG data from Trust M
2. Send TRNG data over USB CDC serial
3. Repeat

There is currently only an [Arduino library](https://github.com/Infineon/arduino-optiga-trust-m) available for talking to the Trust M, so we'll do everything using Arduino.

The complete Arduino sketch code is provided later in this guide to allow customizing. Here we provide some pre-compiled examples in UF2 format. With these UF2 files, you can just drag-and-drop to the Trinkey QT2040 RPI-RP2 bootloader folder. The Arduino IDE does not even need to be installed.

## Installing UF2 Examples

To install the UF2 files:

1. Put the Trinkey QT2040 in bootloader mode by holding the **BOOT** button while pressing the **RST** (reset) button.
2. A folder named **RPI-RP2** should appear.
3. Drag the **UF2** file to the **RPI-RP2** folder.
4. Once copied, board should reset and code is now running.

## Send Raw Bytes

This example sends a continuous stream of raw bytes. This behaves much like dumping `/dev/random` on a linux machine. Here's the UF2:

[TRNG_RAW.uf2](https://cdn-learn.adafruit.com/assets/assets/000/107/111/original/TRNG_RAW.uf2?1638910723)
With that running on the Trinkey QT2040, the serial output will look like this ([screen](https://www.gnu.org/software/screen/) was used to connect):

![](https://cdn-learn.adafruit.com/assets/assets/000/107/108/medium800/sensors_Screenshot_from_2021-12-07_12-42-29.png?1638910051)

 **It's raw bytes, so will look like gibberish.**

Want something more human readable? OK, let's try some formatted string output next.

## Send CSV Formatted Text

This example provides comma separated (CSV) formatted text. Here's the UF2:

[TRNG_CSV.uf2](https://cdn-learn.adafruit.com/assets/assets/000/107/112/original/TRNG_CSV.uf2?1638910739)
With that running on the Trinkey QT2040, the serial output will look like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/107/109/medium800/sensors_Screenshot_from_2021-12-07_12-49-05.png?1638910163)

## Send JSON Formatted Text

This example provides JavaScript Object Notation (JSON) formatted text. Here's the UF2:

[TRNG_JSON.uf2](https://cdn-learn.adafruit.com/assets/assets/000/107/113/original/TRNG_JSON.uf2?1638910753)
With that running on the Trinkey QT2040, the serial output will look like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/107/110/medium800/sensors_Screenshot_from_2021-12-07_12-50-12.png?1638910287)

# Trinkey True Random Number Generator

## Receiving TRNG

The TRNG data can be received by anything that can interact with the USB CDC serial port that gets created. In the previous section, we used [screen](https://www.gnu.org/software/screen/) to simply open and view the output, without doing any parsing or processing. Here we go a little further and show some simple examples of how to do this using Python.

## Install pySerial

The pySerial module is used to open and read from the serial port in Python. If this module is not already installed on your setup, go here:

[pySerial](https://pypi.org/project/pyserial/)
## Read Raw Bytes

This is the simplest thing to read. The out is just a continuous stream of random bytes. You can read in as many of these as you wish.

Here is how to read in 4 bytes.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Trinkey_QT2040_TRNG_Gadget/python/trng_read_raw.py

![](https://cdn-learn.adafruit.com/assets/assets/000/107/118/medium800/sensors_Screenshot_from_2021-12-07_13-11-28.png?1638911499)

## Read CSV String

Reading and parsing a comma separated (CSV) formatted string is also easy, thanks to Python's excellent string handling capabilities. Once parsed, the data can be further processed for whatever end application.

Here we just turn it into a list (array) of integers.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Trinkey_QT2040_TRNG_Gadget/python/trng_read_csv.py

![](https://cdn-learn.adafruit.com/assets/assets/000/107/117/medium800/sensors_Screenshot_from_2021-12-07_13-10-35.png?1638911450)

## Read JSON String

[JavaScript Object Notation](https://en.wikipedia.org/wiki/JSON), aka JSON, is another common text based data format. It allows for more complexity than CSV and thanks to [Python's JSON](https://docs.python.org/3/library/json.html) module, it is also easy to process. Once loaded in, that data can be accessed via the **trng** entry.

Here is a simple example.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Trinkey_QT2040_TRNG_Gadget/python/trng_read_json.py

![](https://cdn-learn.adafruit.com/assets/assets/000/107/116/medium800/sensors_Screenshot_from_2021-12-07_13-09-17.png?1638911377)

# Trinkey True Random Number Generator

## Customizing

An Arduino sketch is used to create the TRNG sending code that runs on the Trinkey QT2040. Scroll to the bottom of this page for the complete code listing.

You can customize the TRNG sending behavior by modifying these lines found at the top of the sketch:

```cpp
//--| User Config |-----------------------------------------------
#define TRNG_FORMAT   2         // 0=raw, 1=CSV, 2=JSON
#define TRNG_LENGTH   8         // random number length in bytes, 8 to 256
#define TRNG_RATE     100       // generate new number ever X ms
#define BEAT_RATE     1000      // neopixel heart beat rate in ms, 0=none
#define BEAT_COLOR    0xADAF00  // neopixel heart beat color
//----------------------------------------------------------------
```

Here is a summary of the options:

- **TRNG\_FORMAT** - sets the desired output format on the serial port
- **TRNG\_LENGTH** - sets the desired length in bytes
- **TRNG\_RATE** - sets how often new values are sent over serial
- **BEAT\_RATE** - sets NeoPixel heart beat rate
- **BEAT\_COLOR** - sets NeoPixel heart beat color

The UF2 files provided earlier in this guide are simply pre-compiled versions of this sketch with different settings.

## Infineon Trust M Arduino Library

The Arduino library used is provided by Infineon and can be found here:

[Infineon OPTIGA™ Trust M Arduino library](https://github.com/Infineon/arduino-optiga-trust-m)
It can be installed via the Library Manager. Just search for "optiga trust m" and it should show up:

![](https://cdn-learn.adafruit.com/assets/assets/000/107/099/medium800/sensors_lib_install.jpg?1638901796)

## Manually Installing Trust M Arduino Libary

At the time of this guide writing, there is a needed update to the Infineon Trust M library. While this change has been added ([PR merged](https://github.com/Infineon/arduino-optiga-trust-m/pull/8)) into the library code, it has not been released.&nbsp; With the current release version (v1.1.0), a compile time error will occur:

```cpp
error: no return statement in function returning non-void [-Werror=return-type]
```

Therefore, manually installing the library is required.

To download a zip of the library:

1. Go to the [library repo](https://github.com/Infineon/arduino-optiga-trust-m)
2. Click the **Code** button
3. Click **Download ZIP**
4. In the Arduino IDE, install via S **ketch \> Include Library \> Add .ZIP Library...**

![](https://cdn-learn.adafruit.com/assets/assets/000/107/173/medium800/sensors_man_lib_install.jpg?1639079537)

## Arduino Sketch

Here is the code listing for the Arduino sketch used.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Trinkey_QT2040_TRNG_Gadget/trinkey_rp2040_trust_m_trng/trinkey_rp2040_trust_m_trng.ino


## Featured Products

### Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT

[Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT](https://www.adafruit.com/product/5056)
It's half USB Key, half Adafruit QT Py, and a lotta&nbsp;RP2040_..._it's&nbsp; **Trinkey QT2040** , the circuit board with an RP2040 heart and Stemma QT legs. Folks are loving the [QT Py 2040](https://www.adafruit.com/product/4900) we made, but maybe you...

In Stock
[Buy Now](https://www.adafruit.com/product/5056)
[Related Guides to the Product](https://learn.adafruit.com/products/5056/guides)
### Adafruit Infineon Trust M Breakout Board - STEMMA QT / Qwiic

[Adafruit Infineon Trust M Breakout Board - STEMMA QT / Qwiic](https://www.adafruit.com/product/4351)
This is a STEMMA I2C&nbsp;breakout for the [Infineon OPTIGA TRUST M SLS 32AIA](https://www.infineon.com/cms/en/product/security-smart-card-solutions/optiga-embedded-security-solutions/optiga-trust/optiga-trust-m-sls32aia/).

OPTIGA Trust M is the next generation of Trust X....

In Stock
[Buy Now](https://www.adafruit.com/product/4351)
[Related Guides to the Product](https://learn.adafruit.com/products/4351/guides)
### Stacking M2.5 Hardware Kit for STEMMA QT and RP2040 Trinkey

[Stacking M2.5 Hardware Kit for STEMMA QT and RP2040 Trinkey](https://www.adafruit.com/product/5248)
&nbsp;

Here is the perfect hardware kit to make a [RP2040 QT Trinkey](https://www.adafruit.com/product/5056) into any kind of USB-connected smart sensor with a QT-sensor-sandwich!&nbsp;You can assemble it with just a plain Phillips screwdriver. Takes less than a minute to bolt...

In Stock
[Buy Now](https://www.adafruit.com/product/5248)
[Related Guides to the Product](https://learn.adafruit.com/products/5248/guides)
### STEMMA QT / Qwiic JST SH 4-Pin Cable - 50mm Long

[STEMMA QT / Qwiic JST SH 4-Pin Cable - 50mm Long](https://www.adafruit.com/product/4399)
This 4-wire cable is&nbsp;50mm / 1.9" long and fitted with JST SH female 4-pin connectors on both ends. Compared with the chunkier JST PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and remove.

<a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4399)
[Related Guides to the Product](https://learn.adafruit.com/products/4399/guides)

## Related Guides

- [Adafruit Trinkey QT2040](https://learn.adafruit.com/adafruit-trinkey-qt2040.md)
- [Reading QR Codes with the Tiny Code Reader](https://learn.adafruit.com/reading-qr-codes-with-the-tiny-code-reader.md)
- [DIY Trinkey No-Soldering USB Air Quality Monitor](https://learn.adafruit.com/diy-trinkey-no-solder-air-quality-monitor.md)
- [Feather TFT STEMMA Case](https://learn.adafruit.com/feather-tft-stemma-case.md)
- [Program RP2040 in Arduino](https://learn.adafruit.com/rp2040-arduino-with-the-earlephilhower-core.md)
- [Adafruit Monochrome 1.12" 128x128 OLED Graphic Display](https://learn.adafruit.com/adafruit-monochrome-1-12-in-128x128-oled.md)
- [HalloWing Magic 9 Ball](https://learn.adafruit.com/hallowing-magic-9-ball.md)
- [Adafruit QT Py SAMD21](https://learn.adafruit.com/adafruit-qt-py.md)
- [Adafruit TMP007 Sensor Breakout](https://learn.adafruit.com/adafruit-tmp007-sensor-breakout.md)
- [Guitar Synth with CircuitPython SynthIO](https://learn.adafruit.com/guitar-synth-with-circuitpython-synthio.md)
- [Adafruit MSA301 Triple Axis Accelerometer](https://learn.adafruit.com/msa301-triple-axis-accelerometer.md)
- [Adafruit I2C QT Rotary Encoder](https://learn.adafruit.com/adafruit-i2c-qt-rotary-encoder.md)
- [Adafruit Pi Stemma QT Breakout](https://learn.adafruit.com/adafruit-pi-stemma-qt-breakout.md)
- [Qualia S3 Compass](https://learn.adafruit.com/qualia-s3-compass.md)
- [USB C Power Delivery Monitor](https://learn.adafruit.com/usb-c-benchtop-power-supply.md)
