Skip to main content Skip to navigation

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!

Solo Motor Connections

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’t connected directly together as this will cause a short circuit!

Breadboard-Battery Setup

  

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

Microbit-computer

Step 4: Attach jumper cables from Pins “0” and “GND” on the micro:bit to the positive (red) and negative (black) breadboard rails respectively:

Microbit with computer + breadboard 

Step 5: Test your motor spins by connecting it to the breadboard, then make sure it’s disconnected again whilst we program the micro:bit. The final circuit should look something like the one below:

Completed circuit

Note: It’s important you set the circuit up somewhere where the components won’t be disturbed as this may affect your voltage readings.

 

Get Coding!

We will now program the micro:bit to track our battery’s voltage level until it dies (~1V) and store the data to be analysed in tomorrow’s session. To start, launch Microsoft MakeCode on your computer, click “New Project”, and give it a name (e.g. Characterisation Activity) and hit “Create”. You should then have a project open with the following two empty blocks:

MakeCode starting blocks

 

 

Step 1: Right click and delete the “forever” code block and replace with two “on 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 “on start” block will execute when the micro:bit first starts up.

Input blocks + module list

Example of modules (left), starting blocks you should have (right)

Step 2: Create a new variable by selecting the variable module and ‘Make a Variable...’. Call this variable ‘running’ and select OK.

Set running blocks

 

Then input a logic statement (from the logic module), into the “set running to” blocks. Our “running” 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 ‘while’ loop block from the loops module with our ‘running’ variable inserted. We will just use button B to stop capturing.

Set running blocks + while blocks and true/false logic

Note: Since our “running” variable is set to FALSE when the B button is pressed, the green “while running” block will not execute any longer and hence data capture will stop.

Step 3: Add “show string” blocks, from the basic module, which will print text to the micro:bit to tell us the program’s status. Here I have chosen “S” for starting data capture, “F” for finishing, and “Running” whilst the program is running.

Show string blocks

 

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’ve 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:

Gathering/converting data blocks

 

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.

Voltage conversion tip

 

  

Step 5: We need to write these voltage values to a serial value (which I’ve called “V” 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:

Final code example

Step 6: With your micro:bit connected to your computer and your code written, hit the “download” 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 “S”) – this means your micro:bit is ready to collect data!

Fullscreen final code

 

Step 7: Now connect your motor back up, press the A button on your micro:bit and you should see a “Show 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).

Microbit console graphic

 

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 “Save As” and save your data somewhere memorable for tomorrow’s activity.

 

Important: If you need to “go 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 “go back”!

 

Note: Make sure you disable your computer’s sleep settings for optimal data collection! If you’re unsure how to do this, google “how to disable computer sleep mode” followed by your operating system e.g. Windows 10/MAC.

Additional Note: Don’t forget to eject your micro:bit from your computer before removing your USB cable!

Eject notification