**Lesson 10: Decrypt Binary with an ****AI Machine Vision Sensor**

There are only 10 types of people in the world-- those who understand binary, and those who don’t. If you think there is a mistake in this joke, then you must be the latter type of person. The pun is that the binary translation for decimal digit 2 is “10”. Not decimal TEN, but a binary one-zero. People who understand binary codes would know that the actual meaning of this line is “There are only 2 types of people in the world....”

In science fiction or hacker movies, we often encounter computer screens similar to the one shown above. If we look closely, we will notice that the program code displayed on these screens consists of only two digits: 1 and 0. This is the fundamental number system used in computers known as binary. But how are these binary numbers converted into decimal? In the following tutorial, we will explore the conversion methods between binary and decimal using the AI machine vision sensor HuskyLens and micro:bit!

**Learning Objectives**

1. What is binary?

2. How to count in binary

3. Binary and decimal conversion

**Preparation**

**Learning Content**

Before learning binary, it is useful to briefly discuss the more familiar decimal counting system as a frame of reference.

Decimal counting uses ten symbols 0 through 9. Counting begins with the incremental substitution of the least significant digit (rightmost digit) which is often called the first digit. When the available symbols for this position are exhausted, the least significant digit is reset to 0, and the next digit of higher significance (one position to the left) is incremented (overflow), and incremental substitution of the low-order digit resumes. This method of reset and overflow is repeated for each digit of significance. Counting progresses as follows:

000, 001, 002, ... 007, 008, 009, (rightmost digit is reset to zero, and the digit to its left is incremented)

010, 011, 012, ...

...

090, 091, 092, ... 097, 098, 099, (rightmost two digits are reset to zeroes, and next digit is incremented)

100, 101, 102, ...

**Learn Binary**

**1. What is a binary number?**

The binary system works the same way as decimal, except that only the two symbols 0 and 1 are available. And instead of multiplying the digit by a power of 10, we multiply it by a power of 2.

Thus, after a digit reaches 1 in binary, an increment resets it to 0 but also causes an increment of the next digit to the left:

0000,

0001, (rightmost digit starts over, and next digit is incremented)

0010, 0011, (rightmost two digits start over, and next digit is incremented)

0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented)

1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 ...

You may not believe it! Everything you see from mobile phones and computers, whether it is text, pictures, movies, games, or the document you are looking at now, is composed of s 0 and 1.

**2. Why do computers ultimately use binary?**

1.Binary is convenient for the realization of hardware technology. We know that for general circuits, there are only two states: on and off, and binary also only has two symbols: 0 and 1.

2.Binary is more suitable for complex computer logic operations. The two values, 0 and 1, correspond exactly to the two states of computer logic algebra "true" and "false".

3.Binary can simplify mathematical operations. Take addition as an example. For binary, there are only two values, 0 and 1, and there are only three combinations [0+0], [0+1], and [1+1], while the decimal system has 55 arithmetic combinations.

**How does binary count?**

Suppose we were traversing to an ancient time. Now, we were on two beacon towers.

We have agreed that when the enemy comes, we will notify each other and ask for support. When one enemy comes, light the torch once. In this way, we saw the light once, which proved that there was only one enemy and no support was needed. What if 5 enemies come at the same time? There are two ways to convey information:

1.Light and extinguish the torch 5 times.

2.Light 5 torches at the same time.

When I received the signal, I knew that you were facing 5 enemies, and it was enough to send 3 soldiers to support you. But what if two hundred enemies come? According to the above two methods, the enemy has already approached the city until the torches have been lighted 200 times; if 200 torches are lit at the same time, it may cause a fire in the city and the enemy could win before arrival.

Now it is certainly not realistic for us to use torches to transmit signals, but what about replacing torches with signal lights? Although the signal light can transmit the signal by turning on/off quickly, when a large number of enemies come, it is still not convenient. Do you have a better way to transmit the signal?

The "on" and "off" of the bulb are actually two states. If a few bulbs can be used to indicate all possible situations, isn’t it convenient and simple?

The following situations may occur when two indicators are used for signal transmission:

By analogy, using such 8 light bulbs, by setting the "on" or "off" of the light bulbs in different positions, the situation of more than 8 enemies coming can be expressed.

**How to convert between binary and decimal**

There is a certain degree of difficulty in the method of converting between binary and decimal. Here we mainly introduce how to convert binary to decimal.

**1. Binary to decimal**

The conversion of binary to decimal is to sum according to the addition rules of decimal. That is, multiply the digits starting from the least significant bit by powers of 2 (20, 21, 22 and so on) till the most significant bit and then sum up the results.

Take the binary 10000101 as an example:

10000101=1*2⁷+0*2⁶+0*2⁵+0*2⁴+0*2³+1*2²+0*2¹+1*2º

=128+0+0+0+0+4+0+1

=133

Therefore, the decimal number of the binary 10000101 is 133.

Note: The binary number system employs 2 as the base and requires only two different symbols for its digits, 0 and 1. In the binary system, each digit represents an increasing power of 2, with the rightmost digit representing 20, the next representing 21, then 22, and so on. The table below can be used as a reference:

Through binary numbers, a specific number of signals can be transmitted. If all 8 light bulbs are lit, how many enemies are there? When the 8-digit binary numbers are all 1, that is, 11111111, what number does it represent in decimal?

**2. Decimal to binary**

The conversion of a decimal integer to a binary integer is as follows:

Step – 1 Divide the decimal number which is to be converted by two which is the base of the binary number.

Step – 2 The remainder which is obtained from step 1 is the rightmost bit of the new binary number.

Step – 3 Divide the quotient which is obtained from step 2 and the remainder obtained from this is the second rightmost bit of the binary number.

Step – 4 Repeat the process until the quotient remains zero.

Step – 5 The last remainder obtained from the division is the leftmost bit of the binary number. Hence arrange the number from leftmost bit to the rightmost bit (i.e., from bottom to top).

This method can easily be understood by considering an example which is explained below. Take 25 as an example:

**Note: The order of binary numbers is from bottom to top, from high digit to low digit, so the binary number of 25 is 11001.**

**AI Vision Project Practice**

This project only needs two tag paper cards. For the demonstration of the tag recognition function, please refer to Lesson 7. First of all, we default that those two paper cards are learned and can be recognized by HuskyLens. Press the Bistable Button, when HuskyLens recognizes ID1, the screen will display binary 1; when HuskyLens recognizes ID2, it will display binary 0 on the screen. Press the button again to convert the binary data into the familiar decimal number.

**Task1: Display binary**

**Hardware connection**

**Program design**

**Function instruction：**

By recognizing the learned tags, the binary numbers 0 and 1 are displayed on the HuskyLens screen. How can a complete set of binary numbers be displayed? Each time a tag is recognized, a number is displayed, and the following number should not cover the previous number. It needs to be controlled by changing the X coordinate value of the displaying position.

**Flowchart Analysis**

**The MakeCode Sample Program**

**Makecode Sample Program link **: https://makecode.microbit.org/_L6D3KEW3VR0A

**AI Vision Project Operating Effect**

Run the program, when HuskyLens recognizes the tag ID1, the binary number 1 is displayed on the screen; when HuskyLens recognizes the tag ID2, the binary number 0 is displayed on the screen, and the latter number is positioned behind the former one.

**Task2: Decimal to binary**

**Hardware connection**

**Program design**

**AI Vision Function Explain:**

This task will perform function encapsulation according to the three different functions of obtaining binary, converting binary to decimal, and displaying characters. Obtaining binary: After pressing the switch, you can perform tag recognition to obtain the binary number; Converting binary to decimal: press the switch again to convert the binary number to decimal; Displaying: display the decimal numbers on the screen when the button is pressed.

**Flowchart Analysis**

**Note: The flowchart about the three sub-functions is omitted here. Students who are interested can draw the flowchart of the sub-function according to the complete program example.**

**The MakeCode Sample Program**

**Makecode Sample Program link : ****https://makecode.microbit.org/_6Uf0PqH8dLz9**

**AI Vision Project Operating Effect**

Run the program and press the button. When the indicator is on, the binary number is obtained through the tag. Press the button again, the indicator is off, and display the corresponding decimal number on the next line. Take the binary 100101 as an example, verify whether the decimal number of it is 37.

**Project Review**

In this project, we learned how to convert between binary and decimal, and use the tag recognition function of HuskyLens to obtain binary numbers, and then convert the obtained binary numbers into decimal numbers. After mastering the binary conversion method, you can play some games with your friends and give them a string of binary numbers to see if he or she understands what you mean.

**Project Development**

In the binary part, we mentioned that all the text that we see on a computer or mobile phone is composed of binary. What does this mean? In fact, a set of binary numbers can not only be converted into decimal numbers, but also correspond to the corresponding characters (text). Let's use a table to understand the relationship between binary numbers and characters.

Click Buy Study Pack of HUSKYLENS for micro:bit

Click Buy Huskylens - An Easy-to-use AI Camera | Vision Sensor

Click Buy Study Pack with HUSKYLENS and micro:bit V2

Engage in learning the next AI vision project : **HuskyLens Camera with an AI Machine Vision Sensor**

Engage in learning the previous AI vision project : **Safety Helmet Reminder with an AI Machine Vision Sensor**

**License**

**0**