Go Down

Topic: 5V Arduino (Leonardo/Pro Micro) acting as a push button for a raspberry pi zero (Read 3310 times) previous topic - next topic

tinp

Hello everyone,

I am fairly new to Arduino, especially the circuitry part of it, so please forgive me if my question is stupid.

What I want to do is have my Arduino Pro Micro act as a push button for my pi in order to run a script on it under certain conditions.

It has come to my understanding that my Arduino operates exclusively on a 5V logic level, whereas the GPIO pins on the Pi operate on 3.3v. But I have seen very different approaches for solving that problem. Most people use logic shifters, but I have also seen the use of Resistors, which makes alot more sense to me for my use case, as I don't really want to send any real information.

The configuration I have seen is having a digital pin of the arduino connect to a "normal" digital pin of the pi, for example Pin40(GPIO21) through a 300Ohm Resistor, connect the Pin on the Pi to ground through a 1K Ohm resistor and the Grounds of the pi and arduino connected as well.

Then I would only have the Pin on the Arduino have set to HIGH for the Pi to trigger.

Is that accurate?

Thank you for your help.

xl97

A Couple things...

Is there a reason you state Pro-Micro?

The Pro-Micro 'bonus' is that it can act as a HID device when connected to a computer/device that accepts keyboard/mouse input.

It doesnt seem that you want to do this (by connecting to the USB port of the PI and acting as a keyboard/mouse.



* There are also +3.3v versions of the Pro-Micro and Pro-Mini for that matter.  (for only a couple bucks USD of ebay)

It 'seems' that a simple +3.3v Pro-Mini could do what you want without the need for any level/logic shifting.  (of course all GNDs will need to still be connected)



tinp

I wrote Pro Micro because it said so on the board, it is the one I already have. The reason I got it is because of the small footprint and because it was 2$ that day.

Unfortunately, I have to use a 5v arduino, as it is monitoring the +5V and +5VSB pins on the psu in my pc and reacting to their values.

Grumpy_Mike

Quote
Unfortunately, I have to use a 5v arduino, as it is monitoring the +5V and +5VSB pins on the psu in my pc and reacting to their values.
No you don't. You could do it all on the Pi if you want, what ever it is.


You can plug the Arduino directly into the USB socket of the Pi and inject stuff directly into the Pi as if it had been typed in at the keyboard.

tinp

could you please tell me how? The thing is, I am trying to keep the footprint as low as possible and the rpi zero's one usb port is already in use, having a hub introduced to the whole thing would almost double the space necessary at the moment. You actually wrote the post i was referring to, with the 300 and 1K resistors.

What I want to do is to check if the PSU is outputting 5V on the Standby pin but not outputting anything on the normal 5V pin. If that keeps going for 7 seconds (so it has to be checked twice), it should trigger a command or bash script on the pi.

Code: [Select]
int count = 0;
//A0 = 5VSB
//A1 = 5V


void setup() {
  pinMode(15, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  delay(2000);

  if (analogRead(A1) < 100) {
    delay(5000);
   
    if (analogRead(A0) > 960) count++;
    else count = 0;
  }
 
  else count = 0;
 
  if (count == 2) digitalWrite(15, HIGH); //2 hits -> trigger command on pi
  if (count == 3) count = 0; //if it keeps hit state rerun the command
}

Grumpy_Mike

Quote
What I want to do is to check if the PSU is outputting 5V on the Standby pin but not outputting anything on the normal 5V pin. If that keeps going for 7 seconds (so it has to be checked twice)
So the voltages you are likely to be reading are either 5V or 0V is that right? There is no possibility of any other values?

Then simply connect them up to two GPIO pins through resistors and have a task that monitors them in essentially the same way as that Arduino code only with high or low. Being a multi threaded operating system this can run as a background task.

tinp

What are the resistor values i need? Also do I achieve that with wiringPi or with a different software?

Grumpy_Mike

The resistors are like I said before.
Wiring Pi is just like the Arduino's digital setup code, so just like the code you posted for the Arduino but with a Python syntax.

By the way I was talking to Gordon ( the author of Wiring Pi ) last weekend at the Pi birthday party.

tinp

So this would be the correct setup then? Again, forgive me for my poor knowledge.

Also, I have never used python before so I am not sure how to program that. Is there an alternative with less code alteration?



Grumpy_Mike

Quote
So this would be the correct setup then?
Yes.

Quote
I have never used python before so I am not sure how to program that.
You can use any language.
I am a bit puzzled because in the first post you were planning to run a script if you saw one GPIO pin driven by an Arduino. Do you know how to do that on the Pi?
If so then why are you having trouble looking at one pin being high and the other being low?
What language were you going to use?

tinp

Thank you for your help.

That is because I had already found a script on the internet that would work. With my initial idea, the Arduino would only be sending the "signal" for about 2 seconds when the 5V was low and SB was high, so the code would only be run once. So I can't just add another if condition to the code I found, I will have to rewrite it, which I expect having troubles with, as I am not very experienced. I'll figure it out though, again, thank you very much for your help. :)

Grumpy_Mike

Quote
That is because I had already found a script on the internet that would work.
What language was it in?
Maybe you could post it.

tinp

this is the code i had found previously, it's written in python.

Code: [Select]
#!/bin/bash

setup ()
{
  echo Setup
  gpio mode 2 in ;
}
setup
while :
do
        result=`gpio read 2`
        if [ $result -eq 0 ]; then
                mplayer -ao alsa:device=hw=2.0 /home/poohead/shutup.wav 2&>1 > /dev/null
        fi
        sleep 0.1
done   



the code I had for the arduino would check the pins every 5 seconds for a the states I wanted and would only trigger if there were 2 hits in a row. I'm not sure how to adapt this code to that and how to add the second if condition. No idea how python works... :(

Grumpy_Mike

Quote
this is the code i had found previously, it's written in python.
No it is not. It looks like a bash script.
It is not by any stretch of the imagination Python.

You need something on the Pi side you can program in and understand.

tinp

No it is not. It looks like a bash script.
It is not by any stretch of the imagination Python.

You need something on the Pi side you can program in and understand.
My apologies, I must seem pretty stupid right now. I misinterpreted the comments in following thread. I also have no idea how bash works other than the simplest one-line scripts. I only have experience in Java and some C++, as well as the combination of languages the Arduino IDE uses, which I would prefer to use. Is that a possibility?

Go Up