I have been trying to get my raspberry pi to output a signal of High or Low, which is then picked up by digital pin 4 on my arduino.
On the raspberry pi, I have connected the output to GPIO 24 and have set up a simple to circuit with an led and 220 ohm resistor in series to check if there was any output. I did manage to get the led to turn on and off due to typing commands into the console on the raspberry pi, so that can't be the problem.
For the raspberry pi side of things, I have installed the WiringPi library and typed these commands:
gpio -g mode 24 out
This set up the GPIO pin and then to turn them off, I used these independently.
gpio -g write 24 1
gpio -g write 24 0
For my code on my arduino, I have adapted a rtc with stepper motor piece of code that I am working on to go with this sub project.
#include <DS3231.h>
#include <Stepper.h>
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32
//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 // 2048
// Init the stepper object with sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
const int raspiPin = 4;
int raspiSwitch = 0;
int Steps2Take;
void setup()
{
// Setup Serial connection
Serial.begin(115200);
pinMode(raspiPin, INPUT);
// Initialize the rtc object
rtc.begin();
// The following lines can be uncommented to set the date and time
// rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY
// rtc.setTime(22, 32, 50); // Set the time to 12:00:00 (24hr format)
// rtc.setDate(3, 6, 2017); // Set the date to January 1st, 2014
}
void loop()
{
// Read the state from the raspi
raspiSwitch = digitalRead(raspiPin);
// Send Day-of-Week
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
// Declare variable for later testing against
String myTime = rtc.getTimeStr();
if (myTime == "17:00:00") {
Serial.println("Rotating stepper motor at " + myTime + ".");
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
} else if (myTime == "08:00:00") {
Serial.println("Rotating stepper motor at " + myTime + ".");
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
} else if (raspiSwitch == 1) {
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1 turn
small_stepper.setSpeed(500);
small_stepper.step(Steps2Take);
delay(1000);
}
delay (1000);
}
When I use the else if statement to turn the stepper motor if there is a high signal from the raspiPin, the motor will turn even when the signal is low.
I used Serial.println to output the state of the raspiSwitch variable to the serial output and for some reason, it changes by itself between 1 and 0. I do not know why and would like to resolve this so I can move onto the next stage of connecting it to the internet on a php webpage.
If anyone could help, I would greatly appreciated it
-
1First delete everything except initializing and printing the value of the pin from a copy of the sketch; especially disconnect the motor driver. Then use something else like a wire connected to Vcc or Ground to source the input, and verify that you are reading the pin you are switching.Chris Stratton– Chris Stratton2017年06月10日 17:31:32 +00:00Commented Jun 10, 2017 at 17:31
-
Is the ground of the Arduino connected to the ground of the Pi?Majenko– Majenko2017年06月10日 21:55:07 +00:00Commented Jun 10, 2017 at 21:55
-
@ChrisStratton I will try that, thank you for your input.Wisaacj– Wisaacj2017年06月11日 07:36:12 +00:00Commented Jun 11, 2017 at 7:36
-
@Majenko I have not currently got the ground from the arduino going to the ground on the pi, but the signal from the pi is grounded on the pi itself.Wisaacj– Wisaacj2017年06月11日 07:36:57 +00:00Commented Jun 11, 2017 at 7:36
-
hackingmajenkoblog.wordpress.com/2016/12/06/…Majenko– Majenko2017年06月11日 07:43:48 +00:00Commented Jun 11, 2017 at 7:43
1 Answer 1
The Arduino and the Raspberry pi should have a common ground. Connect the ground pin of the Arduino to the Raspberry pi. Both boards should be powered by the same power source and connect a pull-down resistor from the digital pin of the Arduino to ground. about 10Kohms might be enough.