# How to connect an external hardware to magni robot?

**URL:** <https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817>\
**Category:** Uncategorized\
**Created:** [May 14, 2021, 8:44am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817 "2021-05-14T08:44:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiho\_bg](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@tiho\_bg](https://forum.ubiquityrobotics.com/u/tiho_bg)\
**Post date:** [May 14, 2021, 8:44am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/1 "2021-05-14T08:44:21Z")

</div>

Hi

I’m using magni silver with a sonar board. I would like to control an external hardware through one relay. Could you please help me with any free raspberry pin and any tutorial how to control the relay with the magni robot? Thank you!

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 14, 2021, 9:24pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/2 "2021-05-14T21:24:14Z")

</div>

Start with this page: [https://learn.ubiquityrobotics.com/GPIO\_lines](https://learn.ubiquityrobotics.com/GPIO_lines)

To add a relay you can use an NPN transistor such as a 2N3604 and a circuit similar to one I found really fast just now such as this: [https://www.geeetech.com/blog/2014/09/4620-raspberry-pi-based-door-controller/](https://www.geeetech.com/blog/2014/09/4620-raspberry-pi-based-door-controller/)

A small MosFet can also be used but just go with the transistor is fine.  
If your relay is really taking a LOT of coil current then you may have to use a MosFet due to the gain of the transistor. If your relay only takes 100ma or less, transistor is likely fine.

How to control the GPIO is you must be sure that GPIO is free and not going to be used by Magni using the web page I gave you. You will also have to initialize the GPIO line as an output and that depends on if you are using python or C++ so tell us what your code will be to drive this relays GPIO line.

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 14, 2021, 9:36pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/3 "2021-05-14T21:36:46Z")

</div>

Just offhand I suggest use of GPIO17.  
You can get little boards that have the driver on them and are compatible with the 3.3V logic levels of the Raspberry Pi. Take a look at [Adafruit.com](http://Adafruit.com) for example. Another option is using a generic relay board seen on EBay that will work with a Raspberry Pi. This one seems about right: [tinyurl.com/ueh5675n](http://tinyurl.com/ueh5675n) (this is a tinyurl link to shorten full link so use it soon).  
In ebay you can also search for ‘1 Channel DC 5V Relay Switch Board Module for Arduino Raspberry Pi’ to find these sort of boards. They have the driver on them already so just hook raspberry pi ground, the GPIO and the power for the relay board as that doc explains.

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 14, 2021, 10:41pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/4 "2021-05-14T22:41:14Z")

</div>

Also please tell me the voltage and current of the thing you are going to try to control. That will help my reply. Thanks.

---

<div class="post-metadata">

**Author:** ![tiho\_bg](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@tiho\_bg](https://forum.ubiquityrobotics.com/u/tiho_bg)\
**Post date:** [May 17, 2021, 10:29am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/5 "2021-05-17T10:29:04Z")

</div>

Thank you for your answers. I would like to control 6 UVC lamps in parallel with one relay (one lamp has the following characteristics - 55W, 0.77A).  
I have a problem with the control of raspberry pins.  
Meanwhile, when I would like to use gpiozero library in my code (from gpiozero import GPIO ), I receive the following error:

Traceback (most recent call last):  
File “/home/ubuntu/catkin\_ws/src/robo\_cleaner/src/cleaner.py”, line 8, in   
from gpiozero import GPIO  
File “/usr/lib/python2.7/dist-packages/gpiozero/ **init**.py”, line 58, in   
from .devices import (  
File “/usr/lib/python2.7/dist-packages/gpiozero/devices.py”, line 63, in   
pin\_factory = \_default\_pin\_factory()  
File “/usr/lib/python2.7/dist-packages/gpiozero/devices.py”, line 54, in \_default\_pin\_factory  
return pkg\_resources.load\_entry\_point(dist, group, name)  
File “/usr/lib/python2.7/dist-packages/pkg\_resources/ **init**.py”, line 542, in load\_entry\_point  
return get\_distribution(dist).load\_entry\_point(group, name)  
File “/usr/lib/python2.7/dist-packages/pkg\_resources/ **init**.py”, line 2569, in load\_entry\_point  
return ep.load()  
File “/usr/lib/python2.7/dist-packages/pkg\_resources/ **init**.py”, line 2229, in load  
return self.resolve()  
File “/usr/lib/python2.7/dist-packages/pkg\_resources/ **init**.py”, line 2235, in resolve  
module = **import** (self.module\_name, fromlist=[‘ **name** ’], level=0)  
File “/usr/lib/python2.7/dist-packages/gpiozero/pins/rpigpio.py”, line 10, in   
from RPi import GPIO  
File “/usr/lib/python2.7/dist-packages/RPi/GPIO/ **init**.py”, line 23, in   
from RPi.\_GPIO import \*  
RuntimeError: This module can only be run on a Raspberry Pi!

How I can resolve this problem? Please, help!

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 18, 2021, 2:48am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/6 "2021-05-18T02:48:51Z")

</div>

Is there a reason you must use that specific gpio library?  
If not see a test script we have that uses GPIO here:

> <https://github.com/UbiquityRobotics/ubiquity_motor/blob/kinetic-devel/scripts/test_pi_gpio.py>

For the 6 lamps you will have significant common mode ground current issues to be careful they do not cause your Pi to reboot. Those are 0.8 amp each so that starts to be a lot of current. If you use a 10 amp relay that is ok but you will have to use a relay driver like I discussed.

When I say ‘common mode currents’ I am assuming you are trying to use power from the Magni? Do the lamps use 12V or what voltage would they run from? If you are using power from Magni then I suggest you have the ground lead from the battery right to your UV lamp ground (I am assumging those are DC? ). If the lamps are inductive be sure to use the back emf diode common in many high current relay driving circuits across those lamps (of course in correct polarity for such a diode). Do NOT try to run the ground from one of the pins of the Pi to such high current directly. Use separate ground for such currents. I am keeping this ‘general’ as I don’t know your setup. You have a small engineering problem that can lead to Pi reboots if you are not careful in how you run ground to those lamps.

---

<div class="post-metadata">

**Author:** ![tiho\_bg](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@tiho\_bg](https://forum.ubiquityrobotics.com/u/tiho_bg)\
**Post date:** [May 18, 2021, 4:28am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/7 "2021-05-18T04:28:30Z")

</div>

Thank you for your answer. With this library the situation is the same. Something is wrong. Please, help  
I added this line to my code but the error is the same  
import RPi.GPIO as GPIO

---

<div class="post-metadata">

**Author:** ![tiho\_bg](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@tiho\_bg](https://forum.ubiquityrobotics.com/u/tiho_bg)\
**Post date:** [May 18, 2021, 1:04pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/8 "2021-05-18T13:04:44Z")

</div>

Hi

I resolved this problem. Now I have a problem with the global variable that I am using in python.

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 19, 2021, 2:06am UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/9 "2021-05-19T02:06:03Z")

</div>

On globals in python 2.7 you can use a real global OR you can define the var in a class and then in member functions of that class use self.myvar but I am not showing that here.  
The ‘clean’ way is to define a class then call it in the python ‘main’ but I’ll not discuss that here.

I will just discuss the simple case of a real global  
I am sort of hacking this and not testing it but this is just to show the basic approach.  
A script that does this with ‘last\_pos’ is here:

> <https://github.com/UbiquityRobotics/ubiquity_motor/blob/kinetic-devel/scripts/test_velocity.py>

For a simple script lets say you want to keep track of a value

Somewhere at the very top of your python script you define that ‘global’ value

last\_pos = 0

You then later define a routine that needs to use that value

def setAValue(value):  
global last\_pos # \<\< THIS IS THE TRICK SO last\_pos USES THE global

```
.... do something with value but you want to save it for some other routine or this one for later

```

last\_pos = value

def showAValue(otherParams):  
global last\_pos # \<\< THIS IS THE TRICK SO last\_pos USES THE global

```
print last_pos

```

…

---

<div class="post-metadata">

**Author:** ![tiho\_bg](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@tiho\_bg](https://forum.ubiquityrobotics.com/u/tiho_bg)\
**Post date:** [May 19, 2021, 2:32pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/10 "2021-05-19T14:32:55Z")

</div>

Hi

Thank you for your answers. The UVC lamps are with the following characteristics: 55W, 0.77A, working voltage - 83V. I will connect 8 lamps in parallel. I corrected my code and now I can control the raspberry pins freely. Sometimes I receive the same error as before: the global values are not defined. Then I have to reboot the robot and everything is OK. Regarding your suggestions it will be better to use a separate battery for the lamps. I will use also a relay board. Is it necessary to use something like a fuse to protect the relay and especially the magni robot? Thank you!

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [May 24, 2021, 7:51pm UTC](https://forum.ubiquityrobotics.com/t/how-to-connect-an-external-hardware-to-magni-robot/817/11 "2021-05-24T19:51:20Z")

</div>

Ah yes, of course you will have to use relays and some high voltage batteries for those.  
Please take proper eye care. Our eyes are not very ‘happy’ with such extreme UV light.  
Very interesting project. Thanks for explaining the powers and voltages in use.
