Register to receive free access to all teacher materials.

# Codes with Binary

This project focuses on how information can be transferred, particularly digitally.

#### Programming Language

Scratch, Python

#### Subjects

Math, Science

#### Grades

4-5, 6-8, 9-12

#### Free Teacher Materials

#### Standards

This project was developed to meet NGSS standards associated with PS4-C Information Technologies and Instrumentation. The specific standards are 4-PS4-3, MS-PS4-3, and HS-PS4-2. Also, binary numbers can be used to help students gain a greater understanding of place value (Common Core math standard 4.NBT.A and 5.NBT.A). At the middle and high school levels, this project is a good opportunity to emphasize the use of functions (in Scratch, custom blocks) in a program and to relate these to mathematical functions (8.F.A and HSF.IF.B).

This project was developed based on a disciplinary core idea from the Next Generation Science Standards. PS4-C. (Information Technologies and Instrumentation) focuses on how information can be transferred, particularly digitally. This project shows how this idea could be implemented at different levels of complexity for different grade levels.

#### Upper Elementary School

In upper elementary school, students can use the Hummingbird kit to create their own codes using LEDs. They can use a table to relate patterns of lights to particular numbers or letters. Students could be introduced to the idea of binary numbers, but that is not essential. The critical idea is that patterns of on/off signals can be used to transfer information.

#### Middle School

At the middle school level, students can work more deeply with the binary number system and learn to convert numbers between the binary and base ten systems. Students can be challenged to ask the user for a word and then encode it using the Hummingbird LEDs.

This conversion happens in a custom block called **ConvertToBinary**. The portion of the block that sets the first LED is shown below; please refer to the CodesWithBinary2.sb2 to see the rest of the block. First, the number being converted to binary is stored in a variable called tempNum. Then the **floor** block is used to find the divisor of tempNum divided by 16 (= 24). This number is either 0 or 1. If it is 1, then the first LED is turned on; otherwise the first LED is turned off. Next, the **mod** block is used to set tempNum equal to the remainder of its previous value divided by 16. After this, the entire process is repeated for 23 = 8, then 22 = 4, and then 21 = 2. After the 21 = 2 step, tempNum is equal to either 0 or 1, and its value is used to set the last LED.

This project is much more challenging than elementary version. Successful completion of this project will indicate that a student thoroughly understands how base ten numbers are represented in binary. Students should not be given the code for completing this project. Rather, the critical part of this project is for students to implement the conversion algorithm themselves. However, math operators such as **floor** and **mod** should be introduced so that students have the tools necessary for this challenge.

This project shows both the capabilities and limits of Scratch as a programming language. This is a good example of a project that stretches the capabilities of Scratch. Students may notice that lists and nested math operators can be awkward in Scratch. This could be an excellent project to motivate a transition to a more advanced programming language.

#### High School

The high school version of this project requires a more advanced programming language than Scratch. The example programs for this project are in Python, but Java or the Arduino programming language could also be good choices. At the high school level, student groups can work in pairs. One group can encode a word or message into binary using LEDs, and the other group can use light sensors to decode the word.

The first computer encodes the word into binary and displays it using five LEDs. These LEDs are mounted in a semicircle. Paper is used to shield the LEDs so that the second computer can read them more easily.

The first computer runs a program called EncodeBinary.py. This program asks the user to enter a word and verifies that the user has entered only letters and no spaces. Then a for loop is used to convert each letter to binary and display it using the five LEDs. In Python, a list is not required to convert each letter to a base ten number. Instead, the built-in **ord()** function can be used to convert each letter in the word to the corresponding unicode number. Then the function **convertToBinary()** is used to convert a base ten number (1-26) into a five-digit binary number. This function returns a list of length 5.

The list returned by **convertToBinary()** is passed to a function named **lightUpBinary()**. This function sets the Hummingbird LEDs using the list of binary values. Each letter is displayed for 13 seconds.

The second computer uses a Hummingbird attached to a servo motor and a light sensor. The servo motor moves the light sensor to each of the five LEDs controlled by the first computer. For each LED, the values measured by the light sensor are used to determine whether that LED is on or off. A little over one second is required to measure each LED. The program waits 2 seconds before and after measuring each letter to remain in sync with the first program.

A single light sensor measurement is not enough to determine whether an LED is on or off. In the **readLights()** function, 100 light sensor measurements are averaged for each LED. This mean is then compared to a threshold to determine whether the LED is on or off. The **readLights()** function returns a list of five binary digits.

The function **convertToLetter()** takes as input a list of five binary digits. It converts this list into a base ten number and then uses the built-in Python function **chr()** to convert that number to the corresponding letter. The second computer continues decoding letters until all the LEDs are off. It then displays the entire word to the user.

This is a challenging programming project. Students must use loops, functions, mathematical operations, and decision statements. This challenge is particularly interesting because a solution can be improved by either changing the software or changing the hardware setup (for example, by shielding the LEDs or moving the light sensor closer to the LEDs). Deciding which option to pursue will give students a better idea of the types of problems encountered in robotics. The encoding group and the decoding group must work closely together to successfully complete this project.