Reading Battery Level with MKR Motor Carrier
Learn how to monitor battery status on the MKR Motor Carrier.
Introduction
In this tutorial we will take a look at how to read the battery levels of a LiPo battery that is connected to the MKR Motor Carrier. We are also going to check how we can use this information to trigger something in the code.
We will do this by first reading the battery levels, and if the battery levels are OK, we will conduct a motor test, where we activate a DC motor. If the battery levels are NOT OK, we simply set the duty to 0, and print information to the Serial Monitor.
Goals
The goals of this project are:
- Set up the MKR Motor Carrier.
- Learn how to read battery levels.
- Create a disengage mode in case battery is too low.
Hardware & Software Needed
- Arduino IDE (online or offline).
- ArduinoMotorCarrier library installed.
- MKR Motor Carrier (link to store).
- Arduino MKR family board (link to store)
- LiPo Battery (2S or 3S).
- DC motor (optional).
- Power Supply (optional).
Note: The DC motor is marked as optional as we are mainly testing the battery reading function in this tutorial.
Note: For this tutorial, we used a 11.1V, 3 cell (3s) LiPo battery.
Useful Scenarios
Knowing the charge status of your batteries can be useful, since you can program your controller to take decisions based on this information. For example, you can blink an LED to give a signal that the battery needs to be recharged or exchanged.
Another useful example is for protecting your batteries. For example, LiPo batteries are very delicate and can be damaged if you continue using them when they are discharged. One safety measure you can add to your project is to turn off all the motors when the battery is discharged.
Circuit
First, we need to mount the MKR family board on top of the MKR Motor Carrier.
Then, we need to add the battery (or power supply) to the male pins. We can also add this to the blue terminal block (VIN and GND) right next to the male pins.
Optional: We can also connect a DC motor to the pins M1+ and M1-, as shown in the image below.
Now, we can turn on the MKR Motor Carrier, by flicking the switch from OFF to ON.
Finally, we can connect a micro USB cable to the MKR board that we are using.
Programming the Board
We will now get to the programming part of this tutorial.
1. First, let's make sure we have the drivers installed for the board we are using. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.
2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for ArduinoMotorCarrier and install it.
3. Here are some of the core functions of this sketch:
- establish communication between board and carrier.controller.begin()
- command to read the battery voltage.float batteryVoltage = (float)battery.getRaw() / 77;
- sets working duty for the motor (0 is min, 100 is max).M1.setDuty()
- keeps the communication between board and carrier active.controller.ping()
The full code for the sketch can be found in the snippet below. Upload the sketch to the board.
1#include <ArduinoMotorCarrier.h>2#define INTERRUPT_PIN 63
4//Variable to store the battery voltage5static float batteryVoltage;6//low battery limit (discharged)7static float batteryLimit; 8//Variable to change the motor speed and direction9int duty = 50;10
11
12void setup()13{14 //Serial port initialization15 Serial.begin(115200);16 while (!Serial);17
18 //Establishing the communication with the motor shield19 if (controller.begin())20 {21 Serial.print("MKR Motor Shield connected, firmware version ");22 Serial.println(controller.getFWVersion());23 }24 else25 {26 Serial.println("Couldn't connect! Is the red led blinking? You may need to update the firmware with FWUpdater sketch");27 while (1);28 }29
30 // Reboot the motor controller; brings every value back to default31 Serial.println("reboot");32 controller.reboot();33 delay(500);34
35 //Take the battery status36 float batteryVoltage = (float)battery.getRaw() / 77;37 Serial.print("Battery voltage: ");38 Serial.println(batteryVoltage);39
40 M1.setDuty(0);41 M2.setDuty(0);42 M3.setDuty(0);43 M4.setDuty(0);44
45 batteryLimit = 9.50; //Around 9V for a 3S LiPo battery46
47}48
49void loop() {50
51 //Take the battery status52 batteryVoltage = (float)battery.getRaw() / 77;53
54 //Reset to the default values if the battery levels are lower than 11V55 if (batteryVoltage < batteryLimit)56 {57 Serial.println(" ");58 Serial.println("WARNING: LOW BATTERY");59 Serial.println("ALL SYSTEMS DOWN");60 M1.setDuty(0);61 M2.setDuty(0);62 M3.setDuty(0);63 M4.setDuty(0);64 while (batteryVoltage < batteryLimit) 65 {66 batteryVoltage = (float)battery.getRaw() / 77;67 controller.ping();68 //wait69 delay(50);70 }71 }72 else73 {74 //Motor test75 M1.setDuty(duty);76 delay(500);77
78 float batteryVoltage = (float)battery.getRaw() / 77;79 Serial.print("Battery voltage: ");80 Serial.println(batteryVoltage);81
82 //Keep active the communication MKR1000 & MKRMotorCarrier83 //Ping the samd1184 controller.ping();85 //wait86 delay(50);87 }88}
Testing It Out
After successfully uploading the sketch, open the Serial Monitor.
In this sketch we are turning the DC motor (M1) ON, and constantly check whether the battery is at an acceptable level.
In case the battery is discharged, we are turning OFF all the motors and sending an error message. This is visible in the Serial Monitor.
Troubleshoot
If the code is not working, there are some common issues we can troubleshoot:
- Check that the circuit is made properly. The circuit section in the top shows exactly how to connect it.
- We are not using a powerful battery (or battery was discharged before we started).
- The ArduinoMotorCarrier library is not installed.
Additional Information
If you have a power supply you can modify the voltage levels to see how the system behaves.
You can test this sketch without the DC Motor. Just check the M1 LED indicator, next to the blue terminal block.
When the systems go down, It will remain in that state until you exchange the battery. So if you have a USB connected, you can turn off the switch, change the battery, connect a charged one and then turn the switch on and the system will proceed as normal.
You can set up the minimum voltage level that you need. For example if you have a 2S (2 cells) LiPo you can set it up to 6V, or if you have a 3S (3 cells) LiPo you can set it up to 9V.
Conclusion
In this tutorial we created a simple, yet powerful way of monitoring a battery's levels. We also created a way of "shutting down" all activities if the battery levels were too low, which is great way of keeping your batteries alive for longer!
Feel free to explore the ArduinoMotorCarrier library further, and try out some of the many cool functions.
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.