---
# System prepended metadata

title: Temperature sensing for LiIon batteries for an Electric mountainboard

---



# Temperature sensing for LiIon batteries for an Electric mountainboard
Mario Nikolov mn224hx

##  Overview
This will take you 5 hours if you do the exact same thing
Github code link: https://github.com/xlordofpainx/IOT-2022

## Objective and description
I made an electric mountainboard. In the battery compartment there is a temp sensor for the controller but the battery box has 4 compartments hence i can only see the temperature un the controller compartment. This is why i wanted to have temp sensors so that i can monitor the temperature of the batteries and look at the data later.
PS: read the entire steps before trying- there may be an explanation at the end of the step. Being able to constantly measure battery temperature can help predict a fire.

## Materials

You need:

* a fipy (or any ESP32 pcb) - 60 euro
![](https://i.imgur.com/r5PFXss.png)
* USB to TTL connector - 5 euro
this is needed because I am not using an expansion board
![](https://i.imgur.com/w4jvV5H.jpg)
* 3 dalas 18B20 temperature sensors - 15 euro
measures from -55 to 125 degrees Celcius
![](https://i.imgur.com/ZMkZAAp.jpg)
* 4.7kOhm resistor - 10cents
![](https://i.imgur.com/RUlZcT6.jpg)
* breadboard and breadboard wires - 5 euro
![](https://i.imgur.com/6OFyX4s.jpg)
* Atom (the IDE I used) - free

I bought everything from https://www.comet.bg/, but a fast google search will give you many results. Also you can google electric component shops near you and check those as well.

All components work with a voltage between 3.3V and 5V.

I did not buy any expansion board or kits because they were overpriced and it made no sense to get those. I got the fypi because i thought that that was the required controller. Later i understood that any microcontroller was fine as long as i was using python. So you can get Another microcontroller with the same chip (USP32 Cam module for example) as the Fypi for 10x lower price that even come with a camera.

(CODE IS AT THE END)

## Computer  and basic setup

I am using Atom as an IDE and windows.

### connecting the USB
1. First I connect the usb converter to the breadboard and to the microcontroller:
![](https://i.imgur.com/5TQVCaO.jpg)
the pins are: 5V GND RX TX (last one is 3.3V not used)
Here is another picture mirrored (from the backside):
![](https://i.imgur.com/dR1GEKA.jpg)
here are the used pins on the Fypi:
![](https://i.imgur.com/SrKXFsG.png)
it is better if you first connect the 5v and GND to the breadboard and from there you connect everything to the breadboard 5V and ground:
![](https://i.imgur.com/ZvvG9sg.png)


2. Then you check in device manager if the usb is showing that there is a device on some of the COMS. If it says that there is an error it means that you have to update the driver for that USB converter. There are many different usb converters so googling the specific error will provide a link with the right driver. This is how it should look if all is well:
![](https://i.imgur.com/vxJSP4N.png).

### FLASHING
3. After connecting the usb you run Pycom Firmware updater following this tutorial:https://hackmd.io/@lnu-iot/SJ91R_jSO

If you don't do this properly. There will be errors when running code from tutorials which runs properly on other people's controllers.

PS! connect pin 4 to GND before flashing to enter BOOTLOADER mode of the FYPI:
![](https://i.imgur.com/4ZxE6VF.jpg)


4. After you have the proper firmware on the Fypi you can intall the IDE (your working environment). I used Atom following this tutorial:https://hackmd.io/@lnu-iot/SydH7MTcw

Now you have the controller and IDE ready to program and connect sensors

## Connecting sensors

Now you connect the 3 sensors the following way following the data sheet:
![](https://i.imgur.com/kSu3O39.png)

1. There are 3 pins so connect the ground to the ground of the Breadboard (GND) (meaning that you first connect the GND of the Fypi to the blue line on the breadboard and the 5V to the red line of the breadboard)
2. Connect the DQ (Data  pin) (the mniddle one) to the resistor and from the resistor to 5V
This is done because the way this sensor works is it always outputs 5V to the controller and when there is data to be transmitted it pulls the 5V data line to ground. But we need the resistor so as to not risk burning a component or pin.
3. Connect the VDD to 5V on the breadboard

Here is how it looks:
![](https://i.imgur.com/rzfosbw.jpg)

Now you have powered the sensor and its ready to send data to one of the pins of the fypi. 

**WHAT ABOUT THE OTHER 2 sensors? Connect them the same way to the same resistor**


## Sensors to FyPi

The only thing left now (hardware wise) is to connect the sensors to some pins. You can connect all of the sensor pins to 1 pin of the controller or to seperate. I chose 3 seperate pins becayse it was easier to deal with  the data strings when they were seperate.

I connecter the middle pins of the 3 temp sensors to pins 
![](https://i.imgur.com/Tpro2ET.jpg).

## Uploading code to FyPi

To upload code in Atom, you chose the project name, you connect the device and after saving the latest changes you press the upload symbol:
![](https://i.imgur.com/N3TFEYi.png)


## Network connection

I chose Wi-Fi because when im riding i can provide wifi to my fypi and get the data to the cloud that way. Anyother method seemed harder or not applicable taking into accouint that i ride all over the country. I followed this tutorial: https://hackmd.io/@lnu-iot/SJ8TGsUd5

I created a boot.py file which runs before anything else and starts the wifi connection:
![](https://i.imgur.com/V4P5ydC.png)

```
import network
import time


WIFI_SSID = "ENTER WIFINAME"
WIFI_PASS = "ENTER WIFI PASS"

# setup as a station
wlan = network.WLAN(mode=network.WLAN.STA)
wlan.connect(WIFI_SSID, auth=(network.WLAN.WPA2, WIFI_PASS))
while not wlan.isconnected():
    time.sleep_ms(50)
print(wlan.ifconfig())
print("connceted")

```

## Displaying the information

To display the data I chose Adafruit and followed this tutorial:https://hackmd.io/@lnu-iot/ByPRkQTF9. I chose Adafruit because it seemed easy enough and was free. I would not chose anything else because this is perfect.
Adafruit is easy to use and has enough options to present your data.
Here is an example of the temperature of one of the batteries:
![](https://i.imgur.com/inwnuHt.png)
![](https://i.imgur.com/N1PchKD.png)

The dip is when i disconnected it for a moment

To send data i made a seperate file for connection to the adafruit server:
![](https://i.imgur.com/W5MTlz1.png)
In it I run the following code to connect to adafruit:
```
# Adafruit IO (AIO) configuration
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = "USERNAME"
AIO_KEY = "The key from the adafruit website"
AIO_CLIENT_ID = bytes('client_'+'12321','utf-8') #random
AIO_CONTROL_FEED = "The name of the specific feed"
AIO_CONTROL_FEED2 = "The name of the specific feed"
AIO_CONTROL_FEED3 = "The name of the specific feed"
#This is for the basic settings: username and keys
#connect()

# Subscribed messages will be delivered to this callback
try:
    client.connect()
    print("Connected to %s, subscribed to %s topic" % (AIO_SERVER, AIO_CONTROL_FEED))
 ```
Then to publish the data :
```
client.publish(topic=AIO_CONTROL_FEED, msg=str(temp1.read_temp_async()))
```

Furthermore to connect to the adafruit server I use MQTT which is a function i implemented as another seperate file:
![](https://i.imgur.com/HgcIvIY.png). The code for this is available in the adafruit tutorial

The libraries used for the adafruit connection are: network,time, mqtt, ubinascii, sys, machine and micropython

The data is send every 5 seconds so that it does not flood with information

## Main code for 1 sensor
The main code with which the sensor data is interpreted and then send to adafruit:
```
ow1 = OneWire(Pin('P10')) #getting data from the sensor
temp1 = DS18X20(ow1) 


while True:
    print(temp1.read_temp_async()) #printing the temperature
    time.sleep(1)
    temp1.start_conversion() #converting to numbers
    time.sleep(1)

    client.publish(topic=AIO_CONTROL_FEED,  msg=str(temp1.read_temp_async()))

#sending the data to the adafruit server
    print("DONE")
    time.sleep(5)
#printing that the sending is done and sleeping for 5 seconds
# this is an infinite while loop that reads the sensor data, converts it and sends it to the adafruit app.
```

## Conclusion

I had a lot of trouble with getting all 3 to work and noone knew why ( we were trying to do it with 1 string on 1 pin)m but after experimenting with manty different ways to write the code it worked.

I had also problem with connecting to adafruit but after playing with the code and reflashing 2 times it worked.

I had problems with the USB but after finding the correct driver for this specific USB it was fine.

To upgrade this i need to make a 3d case for the 3 sensors, add a 3xAAA battery case to power the Fypi and find a way to place it withing the Electric board battery compartment.

