How to connect an external hardware to magni robot?


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!

Start with this page:

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:

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.

1 Like

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 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: (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.

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

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/”, line 8, in
from gpiozero import GPIO
File “/usr/lib/python2.7/dist-packages/gpiozero/”, line 58, in
from .devices import (
File “/usr/lib/python2.7/dist-packages/gpiozero/”, line 63, in
pin_factory = _default_pin_factory()
File “/usr/lib/python2.7/dist-packages/gpiozero/”, line 54, in _default_pin_factory
return pkg_resources.load_entry_point(dist, group, name)
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 542, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 2569, in load_entry_point
return ep.load()
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 2229, in load
return self.resolve()
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 2235, in resolve
module = import(self.module_name, fromlist=[‘name’], level=0)
File “/usr/lib/python2.7/dist-packages/gpiozero/pins/”, line 10, in
from RPi import GPIO
File “/usr/lib/python2.7/dist-packages/RPi/GPIO/”, 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!

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

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.

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


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

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:

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


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!

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.