odroid-xu4:application_note:odroid-hc2_with_minidcups

# ODROID-HC2 with mini DC UPS

A lot of NAS system has a UPS to protect the system which has their valuable data from the blackout or something like the electricity is off accidentally.
To make building the system, I bought a cheap mini DC UPS to use with ODROID-HC2. And I believe it is good enough other than any kind of expensive UPSs.

Mini DC UPS has 12V output.
We make 5V from the output 12v by two resistor voltage divider to measure how many remain the current using ADC under the USB IO Board.

Here is showing you how to make it step by step.

1. 7800 mAH mini DC UPS. You can use any other 12Volt DC UPS if its output is 2Amp or higher.
2. 10K axial resistor
3. 10K Potentiometer
4. Some wires

Mini DC UPS has two parts as I take a big picture, one is PCB and the other is a battery.
The battery connector of the PCB part can measure 12V output that is point how many remain current to the battery.
Soldered the RED wire is +12V, and The Black wire is GND.

USB IO Board can choose either 3.3v(default) or 5v supply by the position of R1. It also becomes ADC reference voltage.
Check it out USB IO Board
I have decided to use 5V ADC reference voltage. It has better get resolution than 3.3V and gets easy calculating.
so, I soldered the R1 resistor to VBUS 5V from 3V3 on the PCB.

Using two resistors 10KOhm(R1) and 7.143KOhm(R2), We can be divided 5V and 7V from 12V in principle.

12V x ( R2 / (R1 + R2) ) = 5V
If R1 is 10,000 Ohm, R2 is about 7,143 Ohm.

However, there is not 7,143 Ohm out there, so I am using a 10K potentiometer
Now I've measured when I got the divided 5V, my R1 is 9.98 KOhm and R2 is 7.44 KOhm that means the mini DC UPS is a few under 12V when I charged fully.
I manipulated to increase the R2 value a little more to get ADC value full 10bit value 1024.

Build software as following the below instruction on ODROID-HC2.

```\$ sudo apt-get install libusb-1.0-0-dev
\$ git clone https://github.com/hardkernel/Odroid-USBIO
\$ cd Odroid-USBIO/usbio/linux
\$ make
\$ sudo ./usbio```
```a. Toggle LED
b. AN0/POT Value
c. Button status
d. Get Register
e. Set Register
f. Get Register Bit
g. Set Register Bit
q. Exit

b
msb = 512, lsb = 212
potentiometerValue = 724```

I've set the maximum ADC value is 1023(10 bits all high) by manipulating R2 when mini DC UPS is charged fully.
We have to know the minimum ADC value to see remaining battery level.
I've acquired this minimum ADC value is 724 by giving some load like stress app to the system supplied power using mini DC UPS only.

This script helps me to get minimum ADC value.

```root@odroid:~/usbio/Odroid-USBIO/usbio/linux# cat -n batCheck.sh
1	#!/bin/bash
2
3	for i in {1..100000}
4	do
5	./usbio << endl >> ./adcValue.log
6	b
7	q
8	endl
9	echo "`date +%Y/%m/%d-%H:%M:%S` : \${i}"
10	sleep 2
11	done
root@odroid:~/usbio/Odroid-USBIO/usbio/linux# nohup ./batCheck.sh &```

Now, We figured out the maximum and the minimum ADC value. Therefore, we can calculate the remaining battery level.

Remaining battery level(%) =
(ADC value - minimum ADC value) x 100 / (1023 - minimum ADC value)

And I want to show you that before arriving the minimum ADC value, it is going to shut down ODROID-HC2 safely.
This is an example shell script how to make an application with it.
As I said before, My minimumADC value is 724 by experimentation, so I set the minimumADC value is 800 by a wide margin in this script.
If remaining battery level of mini DC UPS is lower than 10 percent as I set \${minRemainBat} in the script, it is going to invoke shutdown procedure.

```     1	#!/bin/bash
2
5	minRemainBat=10 # 10% percent
6
7	while true
8	do
9
10	./usbio << endl >> \${logFile}
11	b
12	q
13	endl
14
15	if [ -f \${logFile} ];
16	then
17		ADCValue=`cat /var/log/batADC.log | grep "potentiometerValue" | awk -F" " '{print \$3}' | tail -1`
18
19		if [ \${ADCValue} -gt 0 ];
20	 	then
21
22			# remainBat=`expr \( \( \${ADCValue} - \${minimumADC} \) / \( 1023 - \${minimumADC} \) \) '*' 100`
23			remainBat=`expr \( \${ADCValue} - \${minimumADC} \) '*' 100 / \( 1023 - \${minimumADC} \)`
24
26			echo "Minimum ADC value\$ : \${minimumADC}"
27			echo "Remaining Battery level : \${remainBat}%"
28
29			if [ \${remainBat} -lt \${minRemainBat} ];
30			then
31				echo "Shut down system now"
32				sleep 1
33				/sbin/poweroff
34			else
35				echo "Delete batteryADC log"
36				rm -f \${logFile}
37				sleep 1
38			fi
39	 	fi
40	fi
41	sleep 60
42
43	done```
• odroid-xu4/application_note/odroid-hc2_with_minidcups.txt