Relay Control using Raspberry PI

  • HTML tutorial

WillyGoat

Rank V
Launch Member

Enthusiast III

1,421
Salem, IN
First Name
Stephen
Last Name
Williams
Member #

3258

I am in the process of building a touch panel relay control system and am starting from scratch. I don't like the ones available on the market because of how I want the system to work. Picky, I know. I have figured out that I can use a Raspberry PI computer to control the custom relay system that I am also building from scratch. One problem, I don't know how to program. Anyone out there already have code that I could beg/borrow/steel already setup to do this? I have enough experience that I can figure out how to manipulate code (kind of) but not enough experience in actually writing it. 20 years ago I would never have imagined that this old fart would be integrating programming into building a rig. Who knew... Thank you in advance everyone! I've attached some pix to show the beginning of the system. What you don't see is the automatic switch that disconnects the batteries from each other when the voltage gets too low. This will allow me to maintain a starting battery and let me run the other batter all the way down if I choose to.

IMG_2075.JPG IMG_2091.JPG IMG_2085.JPG IMG_2086.JPG
 
  • Like
Reactions: JimInBC

Boort

Rank V
Launch Member

Member III

2,779
Colorado
Member #

9314

@WillyGoat
I am in the process of building a touch panel relay control system and am starting from scratch. I don't like the ones available on the market because of how I want the system to work. Picky, I know. I have figured out that I can use a Raspberry PI computer to control the custom relay system that I am also building from scratch. One problem, I don't know how to program. Anyone out there already have code that I could beg/borrow/steel already setup to do this? I have enough experience that I can figure out how to manipulate code (kind of) but not enough experience in actually writing it. 20 years ago I would never have imagined that this old fart would be integrating programming into building a rig. Who knew... Thank you in advance everyone! I've attached some pix to show the beginning of the system. What you don't see is the automatic switch that disconnects the batteries from each other when the voltage gets too low. This will allow me to maintain a starting battery and let me run the other batter all the way down if I choose to.
Not my code but should get you going in the right direction:
https://www.instructables.com/id/Automated-cooling-fan-for-Pi/
https://raspberrypi.stackexchange.com/questions/32129/controlling-a-fan-on-a-relay-with-python#52086

Some background python info that I did not find well covered in linked material:
The "import" section at the top of the code is used to add function libraries used later (I always have trouble remembering which ones I need for projects)
  • import os = Add python controls of the Pi operating system
  • import RPi.GPIO as GPIO = Add control of the GPIO pins whinc will take inputs and control your relays
  • import time = Needed if you will be doing any time based controls
  • import datetime = Needed if you will be doing any time based controls
  • import sys = Needed to add basic system information and make it available to your program
additional discussion from a few years ago on building a project like you are discussing:
https://raspberrypi.stackexchange.com/questions/31431/controlling-12volt-off-road-led-lights-with-raspberry-pi-b-and-touchscreen

I'll say if you get stuck check out the great learning resources from Sparkfun (https://learn.sparkfun.com/) and Adafurit (https://learn.adafruit.com/)

Here are some details on adding Scanguage type ODBII readouts if you still have some capacity left on the pi https://www.tacomaworld.com/threads/raspberry-pi-obdii-project-mpg-monitoring-and-more.536192/

Regards
Boort
 
  • Like
Reactions: WillyGoat

tstead60

Rank IV
Launch Member

Advocate II

989
San Diego California
Member #

5931

I'm thinking you could use the raspberry pi's gpio pins to run a bunch of solenoid switches. Use the USB hookup as the communication to the pi itself. It would be pretty easy to code that piece. The tablet piece might be a bit trickier though.
 
  • Like
Reactions: RichieFromBoston

tstead60

Rank IV
Launch Member

Advocate II

989
San Diego California
Member #

5931

I'm thinking you could use the raspberry pi's gpio pins to run a bunch of solenoid switches. Use the USB hookup as the communication to the pi itself. It would be pretty easy to code that piece. The tablet piece might be a bit trickier though.
It occurred to me that you could use the Bluetooth chip on the raspberry pi itself to communicate with a phone or tablet. Youll also have to find a relay that takes 3.3 volts input because that's what the GPIO pins put out. You could use a transistor too boost to 12 volts and connect it to a regular one I guess, so 2 options there
 

slomatt

Rank V

Influencer I

1,723
Bay Area, CA
Cool project.

I suspect the Raspberry Pi can't source enough current or voltage on a GPIO pin to directly drive a relay. If that is the case then you could use the GPIO pin to switch a transistor, and then use the transistor to control the relay.

Or you could by a board already setup for this, it depends on how large of a relay you need.
 

Barclay

Local Expert Southern CA
Launch Member
Member

Influencer I

3,325
Los Angeles
Member #

10140

Ham/GMRS Callsign
K6BDO
WillyGoat's references are a good place to start; I second using python if you're going to use an RPi. I don't think the coding will be your biggest issue, though. There's 3 other things I'd consider.

Firstly, there's the fact that RPi's aren't stateless. This means you want controlled shutdowns, or you'll corrupt your micro SD card and eventually it will stop working. The options I'm aware of are:
- Plug it in somewhere that's always powered even when the car is off.
- Use something like juice4halt.com.
- Follow the somewhat arduous process of making a run-from-RAM version of the OS, like Tiny Core Linux, and then install all the various development tools and libraries you'll need.
- Don't use an RPi, use something like a Cortex M4 (e.g., Adafruit Feather Cortex M4 or Teensy 3.6, you'll want to code the in C++-ish arduino dialect.)

In any case, you'll still have to deal with:

RPi GPIO pins are 3.3V, and can drive between 2 mA and 16 mA. However, the GPIO pins source only up to 50mA from the power rail, so if you want to assert more than 3 pins at 16 mA at the same time, you're going to exhaust that current. And, 16mA is likely too low to energize a relay supporting a relay that support 20-40A (my offhand guess is you need >100mA or so). So, you'd probably need something like an NPN transitor with an alternate power supply to trigger an automotive-scale relay (See https://www.sparkfun.com/tutorials/119). Keep in mind you still need to allocate ~100mA for the TFT screen, but most of that power will be sourced by the power rail directly and only a little going to the GPIO pins to toggle state.

However, I don't think this will be your biggest problem. You biggest enemy will be heat. It's really hard to do right: you want to minimize solar loading for heat ingress, but maximize heat shedding. So, you want the case to reflect at much heat as possible, which argues for light colors and no ventilation. But when it does get hot enough inside, you want ventilation to let the heat exhaust. Or, you want insulation to prevent the heat from entering, but that has the same disadvantage: it will also keep heat in. Or, you can go with active cooling, but then you're also powering a fan ... so you can have cross ventilation where the fan will pull air through, but that will pull in a lot of dust (and maybe heat), but if you only have ventilation on the exhaust side, then you don't get as much air flow with the negative pressure ....

Good luck, let me know if I can help more!
 
  • Like
Reactions: Boort

Barclay

Local Expert Southern CA
Launch Member
Member

Influencer I

3,325
Los Angeles
Member #

10140

Ham/GMRS Callsign
K6BDO
Note: see also: http://www.redgeek.net/2018/08/02/roaddash/

I have a Gen3 I've just finished, that's based on RPi + python + touchscreen, but I still need to get the post together and put up the code. Note sure when I'll get the time to do that, but I can send you code beforehand if you're interested.
 
  • Like
Reactions: Boort