HackerBox 0058: Encode

by HackerBoxes in Circuits > Arduino

5757 Views, 19 Favorites, 0 Comments

HackerBox 0058: Encode

WholeBoxInstruct0058.png

Greetings to HackerBox Hackers around the world! With HackerBox 0058 we'll explore information encoding, barcodes, QR codes, programming the Arduino Pro Micro, embedded LCD displays, integrating barcode generation within Arduino projects, human input device exploits, and more.

HackerBoxes is the monthly subscription box service for enthusiasts of electronics and computer technology - Hardware Hackers - The Dreamers of Dreams.

There is a wealth of information for current and prospective members in the HackerBoxes FAQ. Almost all of the non-technical support emails that we receive are already answered there, so we really appreciate your taking a few minutes to read the FAQ.

Supplies

This Instructable contains information for getting started with HackerBox 0058. The full box contents are listed on the product page for HackerBox 0058 where the box is also available for purchase while supplies last. If you would like to automatically receive a HackerBox like this right in your mailbox each month with a $15 discount, you can subscribe at HackerBoxes.com and join the revolution!

A soldering iron, solder, and basic soldering tools are generally needed to work on the monthly HackerBox. A computer for running software tools is also required. Have a look at the HackerBox Deluxe Starter Workshop for a set of basic tools and a wide array of introductory activities and experiments.

Most importantly, you will need a sense of adventure, hacker spirit, patience, and curiosity. Building and experimenting with electronics, while very rewarding, can be tricky, challenging, and even frustrating at times. The goal is progress, not perfection. When you persist and enjoy the adventure, a great deal of satisfaction can be derived from this hobby. Take each step slowly, mind the details, and don't be afraid to ask for help

Encoding

Encoding.png

Communicating, recording, or manipulating information requires encoding. Since processing, storing, and communicating information are the essence of modern electronics, we have a lot of encoding to worry about.

As a very simple example of encoding, one might represent how many eyes or ears they have by holding up two fingers, or by using the numerals "2" or "][" or using the words "two" or "dos" or "Er" or "zwei". Not actually so simple, right? Encoding used in human language, especially concerning topics like emotions or abstraction, can become highly complex.

PHYSICS

Yes, everything always starts with physics. In electronic systems, we start by representing the simplest of values by electrical signals, usually voltage levels. For example, ZERO may be represented as ground (approximately 0V) and ONE as approximately 5V (or 3.3V, etc.) to make up a binary system of zeros and ones. Even with just ZERO and ONE, there is often ambiguity to resolve. When the button is pressed is that ZERO or ONE? HIGH or LOW? Is the chip select signal "active high" or "active low"? At what time can a signal be read and for how long will it be valid? In communication systems, this is referred to as "line coding".

At this lowest level, the representations are largely about the physics of the system. What voltages can it support, how fast can it transition, how is the laser turned on and off, how do information signals modulate a radio frequency carrier, what is the bandwidth of the channel, or even how do ion concentrations generate action potentials in a neuron. For electronics, this information is often provided in the imposing tables of the manufacturer's datasheet.

The physical layer (PHY) or layer 1 is the first and lowest layer in the seven-layer OSI model of computer networking. The physical layer defines the means of transmitting raw bits over a physical data link connecting network nodes. The physical layer provides an electrical, mechanical, and procedural interface to the transmission medium. The shapes and properties of the electrical connectors, the frequencies to broadcast on, the line code to use and similar low-level parameters, are specified by the physical layer.

NUMBERS

We can't do a lot with just ONE and ZERO, or we would have evolved to "speak" by blinking our eyes at each other. Binary values are a great start though. In computing and communication systems, we combine binary digits (bits) into bytes and "words" containing, for example 8, 16, 32, or 64 bits.

How do these binary words correspond to numbers or values? In a simple 8-bit byte, 00000000 is generally zero and 11111111 is generally 255 to provide 2-to-the-8 or 256 different values. Of course it doesn't stop there, because there are a lot more than 256 numbers and not all numbers are positive integers. Even before computing systems, we represented numerical values using different number systems, languages, bases, and employing techniques such as negative numbers, imaginary numbers, scientific notation, roots, ratios, and logarithmic scales of various different bases. For numerical values in computer systems, we have to contend with issues like machine epsilon, endianness, fixed point, and floating point representations.

TEXT(CETERA)

In addition to representing numbers or values, binary bytes and words can represent letters and other text symbols. The most common form of text encoding is American Standard Code for Information Interchange (ASCII). Of course various types of information can be encoded as text: a book, this web page, an xml document.

In some instances, such as email or Usenet postings, we might want to encode broader information types (such as general binary files) as text. The process of uuencoding is a common form of binary-to-text encoding. You can even "encode" images as text: ASCII Art or better still ANSI Art.

CODING THEORY

Coding theory is the study of the properties of codes and their respective fitness for specific applications. Codes are used for data compression, cryptography, error detection and correction, data transmission and data storage. Codes are studied by various scientific disciplines for the purpose of designing efficient and reliable data transmission methods. Example disciplines include information theory, electrical engineering, mathematics, linguistics, and computer science.

DATA COMPRESSION (removing redundancy)

Data compression, source coding, or bit-rate reduction is the process of encoding information using fewer bits than the original representation. Any particular compression is either lossy or lossless. Lossless compression reduces bits by identifying and eliminating statistical redundancy. No information is lost in lossless compression. Lossy compression reduces bits by removing unnecessary or less important information.

The Lempel–Ziv (LZ) compression methods are among the most popular algorithms for lossless storage. In the mid-1980s, following work by Terry Welch, the Lempel–Ziv–Welch (LZW) algorithm rapidly became the method of choice for most general-purpose compression systems. LZW is used in GIF images, programs such as PKZIP, and hardware devices such as modems.

We are constantly using compressed data for DVDs, streaming MPEG video, MP3 audio, JPEG graphics, ZIP files, compressed tar balls, and so forth.

ERROR DETECTION AND CORRECTION (adding useful redundancy)

Error detection and correction or error control are techniques that enable reliable delivery of digital data over unreliable communication channels. Many communication channels are subject to channel noise, and thus errors may be introduced during transmission from the source to a receiver. Error detection is the detection of errors caused by noise or other impairments during transmission from the transmitter to the receiver. Error correction is the detection of errors and reconstruction of the original, error-free data.

Error detection is most simply performed using transmit repetition, parity bits, checksums, or CRCs, or hash functions. An error in transmission can be detected (but not usually corrected) by the receiver who can then request retransmission of the data.

Error correcting codes (ECC) are used for controlling errors in data over unreliable or noisy communication channels. The central idea is the sender encodes the message with redundant information in the form of an ECC. The redundancy allows the receiver to detect a limited number of errors that may occur anywhere in the message, and often to correct these errors without retransmission. A simplistic example of ECC is to transmit each data bit 3 times, which is known as a (3,1) repetition code. Even though only 0,0,0 or 1,1,1 are transmitted, errors within the noisy channel can present any of eight possible values (three bits) to the receiver. This allows an error in any one of the three samples to be corrected by "majority vote", or "democratic voting". The correcting ability of this ECC is thus correcting 1 error bit in each triplet transmitted. Though simple to implement and widely used, this triple modular redundancy is a relatively inefficient ECC. Better ECC codes typically examine the last several tens or even the last several hundreds of previously received bits to determine how to decode the current small handful of bits.

Almost all two-dimensional bar codes such as QR Codes, PDF-417, MaxiCode, Datamatrix, and Aztec Code use Reed–Solomon ECC to allow correct reading even if a portion of the bar code is damaged.

CRYPTOGRAPHY

Cryptographic encoding is designed around computational hardness assumptions. Such encoding algorithms are intentionally hard to break (in a practical sense) by any adversary. It is theoretically possible to break such a system, but it is infeasible to do so by any known practical means. These schemes are therefore termed computationally secure. There exist information-theoretically secure schemes that provably cannot be broken even with unlimited computing power, such as the one-time pad, but these schemes are more difficult to use in practice than the best theoretically breakable but computationally secure mechanisms.

Traditional cipher encryption is based on a transposition cipher, which rearranges the order of letters in a message (e.g., 'hello world' becomes 'ehlol owrdl' in a trivially simple rearrangement scheme), and substitution ciphers, which systematically replace letters or groups of letters with other letters or groups of letters (e.g., 'fly at once' becomes 'gmz bu podf' by replacing each letter with the one following it in the Latin alphabet). Simple versions of either have never offered much confidentiality from enterprising opponents. An early substitution cipher was the Caesar cipher, in which each letter in the plaintext was replaced by a letter some fixed number of positions further down the alphabet. ROT13 is a is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in the alphabet. It is a special case of the Caesar cipher. Try it here!

QR Codes

QR Codes.png

QR Codes (wikipedia) or "Quick Response Codes" are a type of matrix or two-dimensional barcode first designed in 1994 for the automotive industry in Japan. A barcode is a machine-readable optical label that contains information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application. A QR code uses four standardized encoding modes (numeric, alphanumeric, byte/binary, and kanji) to store data efficiently.

The Quick Response system became popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes. Applications include product tracking, item identification, time tracking, document management, and general marketing. A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera, and processed using Reed–Solomon error correction until the image can be appropriately interpreted. The required data is then extracted from patterns that are present in both horizontal and vertical components of the image.

Modern smartphones will usually automatically read QR Codes (and other barcodes). Simply open the camera app, direct the camera onto the barcode and wait a second or two for the camera app to indicate that it has locked onto the barcode. The app will sometimes display the barcone contents instantly, but usually the app will require selection of the barcode notification to display whatever information has been extracted from the barcode. During the month of June 2011, 14 million American mobile users scanned a QR code or a barcode.

Did you use your smartphone to read the messages encoded on the outside of HackerBox 0058?

Interesting video: Can you fit a whole game into a QR code?

Old timers might remember the Cauzin Softstrip from 80s computer magazines. (video demo)

Arduino Pro Micro 3.3V 8MHz

ProMicro.png

The Arduino Pro Micro is based on the ATmega32U4 microcontroller which has a builtin USB interface. This means that there is no FTDI, PL2303, CH340, or any other chip acting as an intermediary between your computer and the Arduino microcontroller.

We suggest first testing out the Pro Micro without soldering the pins into place. You can perform the basic configuration and testing without using the header pins. Also, delaying soldering to the module gives one less variable to debug should you run into any complications.

If you do not have the Arduino IDE installed on your computer, start by getting downloading the IDE form arduino.cc. WARNING: Be sure to select the 3.3V version under tools > processor prior to programming the Pro Micro. Having this set for 5V will work once and then the device will appear to not ever connect to your PC until you follow the "Reset to Bootloader" instructions in the guide discussed below, which can be a little tricky.

Sparkfun has a great Pro Micro Hookup Guide. The Hookup Guide has a detailed overview of the Pro Micro board and then a section for "Installing: Windows" and a section for "Installing: Mac & Linux." Follow the directions in the appropriate version of those installation instructions in order to get your Arduino IDE configured to support the Pro Micro. We usually start working with an Arduino board by loading and/or modifying the standard Blink sketch. However, the Pro Micro does not include the usual LED on pin 13. Luckily, we can control the RX/TX LEDs. Sparkfun has provided a neat little sketch to demonstrate how. This is in the section of the Hookup Guide entitled, "Example 1: Blinkies!" Verify that you can compile and program this Blinkies! example onto the Pro Micro before moving forward.

Once everything seems to be working to program the Pro Micro, it is time to carefully solder the header pins onto the module. After soldering, carefully test out the board again.

FYI: Thanks to its integrated USB transceiver, the Pro Micro can easily be used to emulate a human interface device (HID) such as a keyboard or mouse, and play around with keystroke injection.

QR Codes on Full Color LCD Display

LCD QR Display.png

The LCD Display features 128 x 160 Full Color Pixels and it measures 1.8 inch on the diagonal. The ST7735S Driver Chip (datasheet) can be interfaced from almost any microcontroller using a Serial Peripheral Interface (SPI) bus. The interface is specified for 3.3V signaling and power supply.

The LCD module can be wired directly to the 3.3V Pro Micro using 7 FF Jumper Wires:

LCD----Pro Micro

GND----GND

VCC----VCC

SCL----15

SDA----16

RES----9

DC-----8

CS-----10

BL-----No Connect

This specific pin assignment allows the library examples to work by default.

The library called "Adafruit ST7735 and ST7789" can be found in the Arduino IDE using menu Tools > Manage Libraries. While installing, the library manager will suggest a few dependent libraries that go with that library. Allow it to install those as well.

Once that library is installed, open up Files > Examples > Adafruit ST7735 and ST7789 Library > graphicstest

Compile and upload graphicstest. It will generate a graphics demo on the LCD display, but with some rows and columns of "noisy pixels" at the edge of the display.

These "noisy pixels" can be fixed by changing the TFT init function used near the top of the setup(void) function.

Comment out the line of code :

tft.initR(INITR_BLACKTAB);

And uncomment the line a couple of lines down:

tft.initR(INITR_GREENTAB);

Reprogram the demo and everything should look nice.

Now we can use the LCD to display QR Codes

Back to the Arduino IDE menu Tools > Manage Libraries.

Locate and install the library QRCode.

Download the QR_TFT.ino sketch attached here.

Compile and program QR_TFT into the ProMicro and see if you can use your phone's camera app to read the generated QR Code on the LCD display.

Some projects using QR Code generation for inspiration

Access Control

QR Clock

Downloads

Flexible Flat Cable

Flex Connectors.png

A Flexible Flat Cable (FFC) is any variety of electrical cable that is both flat and flexible, with flat solid conductors. An FFC is a cable formed from, or similar to, a Flexible Printed Circuit (FPC). The terms FPC and FFC are sometimes used interchangeably. These terms generally refer to an extremely thin flat cable often found in high-density electronic applications like laptops and cell phones. They are a miniaturized form of ribbon cable that usually consists of a flat and flexible plastic film base, with multiple flat metallic conductors bonded to one surface.

FFCs come in a variety of pin pitches with 1.0 mm and 0.5 mm being two common options. The included FPC breakout board has traces for both of these pitches, one on each side of the PCB. Only one side of the PCB is used depending upon the desired pitch, 0.5 mm in this case. Be sure to use the header pin numbering printed on the same 0.5 mm side of the PCB. The pin numbering on the 1.0 mm side does not match and is used for a different application.

The FFC connectors on both the breakout and the barcode scanner are ZIF (zero insertion force) connectors. This means the ZIF connectors have a mechanical slider that is hinged open before the FFC is inserted and then hinged closed to tighten the connector onto the FFC without putting and insertion force on the cable itself. Two important things to note about these ZIF connectors:

1. They are both "bottom contact" which means that the metal contacts on the FFC must face downward (towards the PCB) when inserted.

2. The hinged slider on the breakout is on the front of the connector. This means that the FFC will go under/through the hinged slider. In contrast, the hinged slider on the barcode scanner is on the back of the connector. This means that the FFC will enter the ZIF connector from the opposite side and not through the hinged slider.

Keep in mind that other types of FFC/FPC ZIF connectors have lateral sliders as opposed to the hinged sliders we have here. Instead of hinging up and down, lateral sliders slip in and out within the plane of the connector. Always look carefully before first using a new type of ZIF connector. They are quite small and can be easily damaged if forced outside of their intended range or plane of motion.

Barcode Scanner

Scanner.png

Once the barcode scanner and FPC breakout are connected by the the Flexible Flat Cable (FFC), five female jumper wires can be used to connect the breakout PCB to the Arduino Pro Micro:

FPC----Pro Micro

3------GND

2------VCC

12-----7

4------8

5------9

Once connected, program the sketch barscandemo.ino into the Pro Micro, open up the Serial Monitor, and scan all the things! It can be surprising how many objects around our homes and offices have barcodes on them. You might even know someone with a barcode tattoo.

The attached barcode scanner manual has codes that can be scanned to configure the processor embedded within the scanner.

Hack the Planet

outro.png

We hope you are enjoying this month's HackerBox adventure into electronics and computer technology. Reach out and share your success in the comments below or other social media. Also, remember that you can email support@hackerboxes.com anytime if you have a question or need some help.

What's Next? Join the revolution. Live the HackLife. Get a cool box of hackable gear delivered right to your mailbox each month. Surf over to HackerBoxes.com and sign up for your monthly HackerBox subscription.