Fully Electric Challenge - Will my battery work?
Will my battery work? Circuit building and Coding activity
Activity Overview
In order to gauge the state of charge (SoC) of your rechargeable batteries, you must first be able to measure their voltages and secondly track how these voltages vary as the batteries are charged/discharged. Your micro:bit will be used to read battery voltage and in order to create a change in voltage we are going to connect a single battery to power a geared DC motor until the battery runs out.
You Will Need
- The Fully Electric Challenge Pack, specifically:
- BBC micro:bit GO Pack
- 1 x Geared DC Motor
- 1 x AA Rechargeable Battery
- 1 x AA Battery Holder
- Mini Breadboard (optional, recommended)
- Crocodile Clips x2
- Breadboard Jumper Cables (6+)
- Electrical Tape
- A computer/laptop with internet access
- Scissors
Constructing your circuit
Step 1: Connect positive and negative wires to your motor, preferably by bending the ends of the jumper cables around the motor terminals (two loops) and then taping together with electrical tape. It is recommended you use different coloured wires to keep track of polarity too!

Step 2: Place your chosen battery inside the single battery holder and connect the positive (red) and negative (black) wires to the breadboard as shown:
Important: Make sure the wires aren鈥檛 connected directly together as this will cause a short circuit!

Step 3: Attach crocodile clips (again using different colours) to Pin 鈥0鈥 and Pin 鈥淕ND鈥 on the micro:bit and use the USB cable to connect your micro:bit to your computer/laptop:

Step 4: Attach jumper cables from Pins 鈥0鈥 and 鈥淕ND鈥 on the micro:bit to the positive (red) and negative (black) breadboard rails respectively:
Step 5: Test your motor spins by connecting it to the breadboard, then make sure it鈥檚 disconnected again whilst we program the micro:bit. The final circuit should look something like the one below:

Note: It鈥檚 important you set the circuit up somewhere where the components won鈥檛 be disturbed as this may affect your voltage readings.
Get Coding!
We will now program the micro:bit to track our battery鈥檚 voltage level until it dies (~1V) and store the data to be analysed in tomorrow鈥檚 session. To start, launch Microsoft MakeCode on your computer, click 鈥淣ew Project鈥, and give it a name (e.g. Characterisation Activity) and hit 鈥淐reate鈥. You should then have a project open with the following two empty blocks:

Step 1: Right click and delete the 鈥渇orever鈥 code block and replace with two 鈥渙n button pressed鈥 blocks from the input module – keep one for button A and change the other for button B (see image below). These three blocks are examples of EVENT blocks, they execute code after a specific condition is satisfied. For example, the code you put inside the 鈥渙n start鈥 block will execute when the micro:bit first starts up.

Example of modules (left), starting blocks you should have (right)
Step 2: Create a new variable by selecting the variable module and 鈥楳ake a Variable...鈥. Call this variable 鈥榬unning鈥 and select OK.

Then input a logic statement (from the logic module), into the 鈥渟et running to鈥 blocks. Our 鈥渞unning鈥 variable will either be TRUE or FALSE depending on whether our program is running or not. By default, this variable will be set TRUE. We are going to use button A on the micro:bit to trigger the start of the data capture so we will need a 鈥榳hile鈥 loop block from the loops module with our 鈥榬unning鈥 variable inserted. We will just use button B to stop capturing.

Note: Since our 鈥渞unning鈥 variable is set to FALSE when the B button is pressed, the green 鈥渨hile running鈥 block will not execute any longer and hence data capture will stop.
Step 3: Add 鈥渟how string鈥 blocks, from the basic module, which will print text to the micro:bit to tell us the program鈥檚 status. Here I have chosen 鈥淪鈥 for starting data capture, 鈥淔鈥 for finishing, and 鈥淩unning鈥 whilst the program is running.

Step 4: Create two variables (just like before in the variables module) – one for storing raw data and another for the resulting voltage (which we obtain after some conversion). We now need to read the analogue data from the pin on the micro:bit which corresponds to the positive side of the circuit (we chose Pin 鈥0鈥 in our earlier setup but you could just as easily have chosen Pins 鈥1鈥 or 鈥2鈥 – just make sure your code matches up).
Once we鈥檝e done this we need to convert the raw data into voltage data by first multiplying by 1000 and then dividing through by 340. This is done using the Math module:

Important: Order of operations is very important here, if you divide first and then multiply (see below) your data will be less accurate due to how the micro:bit handles rounding when performing calculations.

Step 5: We need to write these voltage values to a serial value (which I鈥檝e called 鈥淰鈥 for voltage) using a block from the serial module so they can be stored on your computer. We also implement a pause block which dictates how often the micro:bit will collect data. Since we are going to need several hours to drain the battery completely, I would recommend a pause interval of at least 15 seconds (which is inputted as 15000 ms). Your final code should look very similar to what is presented below:

Step 6: With your micro:bit connected to your computer and your code written, hit the 鈥渄ownload鈥 button on the bottom left of the screen and you should see your micro:bit light up with your starting string (in my case the letter 鈥淪鈥) – this means your micro:bit is ready to collect data!

Step 7: Now connect your motor back up, press the A button on your micro:bit and you should see a 鈥淪how console Device鈥 button appear on MakeCode below the image of the micro:bit. Click on it and you should see your data being collected every X seconds (15 in this example).

Provided this is working as described, leave your micro:bit and motor running until the battery has died (this will likely take multiple hours) – once the motor stops turning, you may hit the download icon towards the top right of the screen. Your default worksheet program should then launch (most likely Excel) displaying your .csv data - make sure to click 鈥淪ave As鈥 and save your data somewhere memorable for tomorrow鈥檚 activity.
Important: If you need to 鈥済o back鈥 to your code to make an edit whilst your data is being collected, make sure you download your data before doing so. Otherwise, all of your stored .csv data (displayed at the bottom) will be lost upon clicking 鈥済o back鈥!
Note: Make sure you disable your computer鈥檚 sleep settings for optimal data collection! If you鈥檙e unsure how to do this, google 鈥渉ow to disable computer sleep mode鈥 followed by your operating system e.g. Windows 10/MAC.
Additional Note: Don鈥檛 forget to eject your micro:bit from your computer before removing your USB cable!
