I am working on a project and ideally I would like to start and stop the loop of an Arduino with a Raspberru PI. The idea is to send a signal from the Raspberry PI GPIO pins to the Arduino pins and trigger and ISR as follows:
volatile bool start = false;
void start(){
start = true;
}
void stop(){
start = false;
}
void setup(){
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), start, RISING)
pinMode(3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(3), stop, RISING)
}
void loop() {
if (start) {
//Do my stuff here...
}
}
Any help on how could I accomplish this from a C++ app that takes an argument, (start or stop, 1 or 0)? Or maybe with Python?
I chose to set two different pins on purpose. I am aware that we could have set only one pin and make it work as a trigger with a function "start = !start" however this way I will have a pin than will always start (or continue) the arduino and another to stop (or make sure the arduino is stopped).
One of my main problems is that I do not know how to set the connections between the two boards (one cable for each pair of pins or should we also need a ground?).
What type of impulse would be more appropriate to send from the raspberry PI?
I am also aware that the Arduino works on 5V and the raspberry pi on 3.3V. Should we connect a resistance in series to avoid any over current?
Thank your for any advice or bibliography you may provide,
Alexis
With respect to your concern about the voltage issues: I would address this first because that has to be established before anything else will work.
Indeed, the voltage on the Raspberry Pi is 3.3 volts and typically Arduinos are 5.0 Volt logic. There are voltage level converters available that will convert 3.3. to 5.0 and back. You can get these from common sources such as Adafruit or Sparkfun. Specifically the name is
**4-channel I2C-safe Bi-directional Logic Level Converter - BSS138 $4.00 **
You can wire the communication between the two devices using this simple device. (Shown is the Sparkfun version).
Alternately and possibly simpler would be to purchase an Arduino that can be run at 3.3 Volts. These are available from China on eBay and have a switch that allows the arduino i/o to be either 5V or 3.3V.
It is possible to use resistors to manage the level conversion. You may want to consider this solution if you have the skills to design it.
Related
I'm having trouble trying to make sounds from my photoresistor using MIDI. I have a MIDISPORT USB. I connected MIDI OUT from my breadboard from the MIDI JACK and my MIDI IN isn't connected to anything because I don't know where it should go or if I should just leave it. I'm using Hairless MIDI serial program to connect my arduino with MIDI, they seem to receiving signal from my usb on the arduino side and the Midi side but I don't hear any sounds.
How does garageband come in to this? It doesn't have to be garageband, i just want to make this work and hear the sounds. I made sure garageband is connected to MIDI as well as having HairlessMIDI at serial 9600 and my arduino at serial.begin at 9600. everything seems to look good so far. I just don't know why I don't hear anything. Am I supposed to hear it from garageband, if so how?
here's my arduino code,
byte noteON = 144;//note on command
void setup() {
Serial.begin(9600);
}
void loop() {
MIDImessage(noteON, 60, 100);//turn note on
delay(300);//hold note for 300ms
MIDImessage(noteON, 60, 0);//turn note off (note on with velocity 0)
delay(200);//wait 200ms until triggering next note
}
//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
If I understood your setup correctly, you use:
an arduino with a sensor to generate a signal, this is the instrument
MIDISPORT USB http://www.performanceaudio.com/media/pdf/121/3757_m.pdf to make the link between the instrument and the computer
HairlessMIDI which converts a serial text protocol to midi. http://projectgus.github.io/hairless-midiserial/#getting_started
I am unsure you need the MIDISPORT at all. HairlessMIDI already makes the link between an Arduino program and a MIDI expander application (see tutorial in the link above).
According to this page, the Arduino must use the MIDI Library, and the init code looks like this.
void setup() {
MIDI.begin();
Serial.begin(115200);
}
Note the 115200 baud rate. The serial port on Arduinos must use a specific baudrate, otherwise the host and slave won't be able to talk to each other.
I think you must connect your setup to the MIDI In side on the HairlessMIDI application. If GarageBand is running, you must be able to select it in the drop down list.
I recently bought an Arduino UNO to read the data outputted by my Smart Meter. The meter uses serial communication and I would like to see the values being outputted on my laptop screen. I figured I would need to use the SoftwareSerial library to read the incoming data and print that data on my screen using the hardware serial and the Serial Monitor in the Arduino IDE. To become familiar with (software) serial communication on the Arduino, I reviewed the documentation of the SoftwareSerial library. Problem is, I can't get the most basic example to work and I have been stuck on this for quite a while now. The example code is below, the example can be found here
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
As far as I understand this, the following should happen:
- Type text in Serial Monitor window.
- Serial.read() reads this data and writes it to the Software Serial.
- The Software Serial reads this data and writes it back to Serial.
- That which was written appears on the screen.
But whatever I try, nothing happens. Among things I tried to following:
- Change the baud rate for both software and hardware serial (9600 for instance).
- Tried a different SoftwareSerial library (AltSoftSerial).
- Tried different RX and TX pins for SoftwareSerial.
- Instead of Serial.write(mySerial.read());, store the result in a char first.
I'm probably missing something obvious. I would be grateful to anyone who could shed some light on this or offer an alternative way for me to read the data from my Smart Meter.
Edit
I had no wiring, because the example specified "There is no circuit for this example". I tried all three options suggested by #slash-dev, but none had the expected behaviour:
SoftwareSerial with wires connecting pin 1 to pin 10 and pin 0 to pin 11. It prints strange characters:
Goodnight moon!
Ùniÿhtÿmoÿn!ÿ
nihtmoÿttt
AltSoftSerial with wires connecting 1-8 and 0-9. First prints Goodnight moon! and then it keeps printing Ô당¥�¡Ñ�moon!.
NeoSWSerial with wires connecting 1-10 and 0-11. Same as AltSoftSerial but keeps printing Ôë‹–+ë¡Ñ�j½½¹…j.
The baud rates must be the same on Serial and mySerial.
And you don't describe the connections, so I have to ask: Did you connect a wire from pin 1 (Serial transmit) to pin 10 (mySerial receive), and another wire from pin 0 (Serial receive) to pin 11 (mySerial transmit)? Note how they are crossed.
AltSoftSerial is really the best choice, but it only works on pin 8 (RX) and pin 9 (TX), which would require connecting 8 to 1 and 9 to 0. SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch or other libraries.
My NeoSWSerial library is another alternative. It's almost as efficient as AltSoftSerial, but it works on any two pins. It can transmit and receive at the same time (unlike SoftwareSerial), but it only works at bauds 9600, 19200 and 38400.
EDIT:
I think what you've tried is probably working ok. All the software serial libraries use interrupts for processing individual bits instead of one interrupt per character. When there are other interrupts in the system (millis() TIMER0 or Serial), the bit "calculations" can be affected. This manifests as receiving the wrong byte. Your loopback test makes it especially susceptible because the sending and receiving are synchronized (the initial receive interrupt occurs while the transmit interrupt is starting the next char).
If you just hook 0 to 1, I think it will work, because the UART is able to send and receive at the same time, and it deals with complete characters, not bits. The character interrupts do not disturb the sending or receiving of the bits.
In developing the NeoSWSerial library, I have seen this manifest the same way. I had to use two Arduinos to fully test asynchronously (i.e., not synchronized). In your case, using AltSoftSerial for the SmartMeter should work fine, and you can choose different baud rates. If you are echoing the SmartMeter characters to Serial, be sure you have a higher baud rate on Serial.
This is most likely not related to the issues now 5 years ago, but in my case I was using the Arduino IDE 2.0.0-rc5 which was not writing to the soft serial for unknown reasons. I downgraded to Arduino IDE 1.8.19 and the same serial sketch worked.
I am fighting with ESP8266 wifi module and connecting arduino. After updating firmware to newest version i started to programm arduino to get data incoming from wifi. I saw many examples about maiking webserver via ESP8266 but none of them works for me.
ESP is connected to my Arduino Leonardo:
>
Arduino -> ESP8266
power 3.3V -> vcc
ground -> ground
tx -> rx (via logic level converter 5->3.3V)
rx -> tx (via logix level converter
power 3.3V ->gpio0 (without any resistors)
I made simple sketch:
void setup(void){
Serial.begin(9600);
Serial1.begin(115200);
}
void loop() {
if(Serial1.available())
{
Serial.println("WIFI IS AVAILABLE");
Serial1.println("AT");
delay(1000);
} else {
Serial.println("WIFI not available.");
delay(1000);
}
}
After executing it ESP8266 is powered (red led is on) and also every second blue led (blinks). That makes me sure that in fakt "AT" command is transmited to module. But there are also two issues:
i want to get response from esp - in this case word "OK". I tried Serial1.read() but it only reads one byte. Serial1.readString() makes my messages "wifi not available" and sametimes "wifi is available" as if for a while the connection would be unavailable
after uploading sketch to arduino and having powered esp8266 wifi module is always unavailable - i need to power the module off and on again to have it working.
Anybody please can help me?
What you need to do is change your approach a bit. Do not check if data is available. The trick is to send the module something and then check for data.
Do something like:
while (Serial.available() > 0)
Serial.read();
to clear the buffer before any command you want to send. Then send the command. Then check for data as a response.
Do not rely on that Blue LED as any indication. It is only an indication that the ESP8266 is busy using the WiFi in some sort of way, whether it is doing keepalives, initializing WiFi or whatever. It can be totally unrelated to whatever you are sending. If you do not receive a valid response then you must assume that there is comms issues between you and the module. One thing though is that if that Blue LED never goes off then either the module has frozen or the firmware was corrupted. I have had that many times. I then reload the firmware and usually that fixes it. It usually only happens during development times where I reset, upload code or change wires.
I use mine with an atmega328 on a separate slef-built board and not the one on the Uno and run that board on 3.3v itself and then use a logic level converter between that atmega board and my Uno so that I can program it. But I have had sporadic issues with non-comms but I suspect it might be power related. Be aware that running your Serial via the logic level converter might also be causing comms issues.
Proposed wiring: All pins except RX,TX,VCC and GND goes to VCC via 10K pullup resistors. RX goes to the arduino's TX and TX goes to the arduino's RX. Of course you know where VCC and GND goes.
I am new to Intel Galileo. I am trying simple things. How do I use serial TX(Digital pin 1) and serial RX(Digital pin 0) for communicating with other UART devices ? Which serial port is this UART ?
I tried to connect it by configuring it as uart 0/1/2 but did not work.
void setup() {
Serial1.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial1.println("Hello Galileo");
delay(300);
}
I have never used the Galileo but I was not aware that it had more than 1 serial port. So basing my thinking on the UNO as well as the MEGA, pin 0 and 1 should not be Serial1 but just Serial, meaning that if you connect the TX pin to the RX pin of another device and then, as you posted above, run your code with Serial.begin(9600); and Serial.println.... instead of Serial1.... it should work as far as I know... Also, I sometimes use Serial.Write but I am unsure of what the difference is. I normally would not answer a question I don't know the exact solution to but as there are no answers yet I thought I would give it a try.
I'm using a Gen2 and the latest build of Windows IoT (9600.16384.x86fre.winblue_rtm_iotbuild.150309-0310_galileo_v2). In that build, Serial is the correct object for COM1 on pins D0 and D1 and I've used it successfully.
Serial1 is supposed to be COM2 on D2 and D3, but I get an error when I try to open it. I'm still working on that.
Is possible for Arduino to receive a 5V into a inputPin from a external battery?
If I have a system that has it's own power supply and after an event fires 5V. How can Arduino read this input?
This is an electrical problem, no software issue I presume.
Here is how to proceed:
1) Make sure both boards have the same ground (connect GND together and make sure there is no conflict)
2) Connect your output to an input on the Arduino board (pin 2 e.g.). This connection is preferably done using a resistor, 1 kOhm will be ok.
On software side, just set this pin as input pinMode(2,INPUT); in setup() and then get its value status = digitalRead(2); in loop().