Reading serial data from Arduino fails - serial-port

I have written a program for Arduino that reads some analog signals and sends them to the computer when it receives a command from the master computer. I wondered why this didn't work on the computer it was intended to run on. On my own computer it runs fine.
I uploaded a simple test code in the Arduino.
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()) {
Serial.println(Serial.read());
}
}
This doesn't run on the second computer either. When I use Arduino serial monitor for transfering data, I see the RX led blink but not the TX. With the computer it's working on, I can see both of the leds flash. Arduino receives the data on both computers, but the second computer doesn't receive Arduino's responses. What might be wrong?
Edit. I forgot the Arduino hooked up to the problematic PC for a few minutes and tried it again. Then it worked! It seems like it needed some time to warm up. Why's that?

Sometimes it can take a second for the Arduino and computer to establish the Serial handshake, especially at 9600 baud. I'm glad you got it working!

Related

Arduino Sends Old Data Through Serial

I think my problem would best be explained with an example. Here is some Arduino Code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(millis());
delay(1);
}
...and here is the output:
Now, those numbers at the beginning aren't just your regular old first-bytes-of-serial-data-are-always-messed-up numbers. Those numbers are left over from the last time I opened the serial monitor. How do I know this? Because I counted how many seconds had gone by since I last opened the serial monitor, and it all matched up.
So here's the big questioroonie,
How do I make this stop? It's breaking my java program that's supposed to read data from the Arduino's current "run instance"(I don't know the correct term), and not the last one.
There is nothing wrong with your Arduino. Remember, the USB connection is not a real RS-232 serial interface, it's simulated through USB. And that USB connection is controlled by a chip with its own buffer.
Bad news is, nothing you can do if you're using the USB cable to carry your RS-232 signals, other than to send some "filler characters" to purge the buffer. Too many buffers everywhere :) Send some terminal emulation commands like "clear the screen" and "form feed" etc. to empty the pipeline of stuff in the buffer.
Which reminds me -- we should all quit using the crummy Arduino serial monitor and be vocal about it. Instead, use TeraTerm or Putty or any other good-quality terminal emulator of your choice.
Or connect with real RS232 and an adapter and cable. My development machine has three serial ports, but that's what I do. Often computers today don't even have one handy.

Make sounds from photoresistor on arduino using MIDI on garageband?

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.

SoftwareSerial example not working as expected

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.

arduino suddenly shows "avrdude: ser_open(): can't open device "\\.\COM3" after last upload

I am using arduino uno to make a sound detector.
I uploaded a program, found error in the code that it returns unintended numbers unreasonably big. I also think I used wrong code for the module, but it was connected in the way that can work properly with the proper code.
The code I uploaded was:
const int ledPin =13;
const int middleValue = 512;
const int numberOfSamples =128;
int sample;
long signal;
long averageReading;
long runningAverage = 0;
const int averagedOver = 16;
const int threshold=400;
void setup(){
pinMode(ledPin, OUTPUT)
Serial.begin(9600)
}
void loop(){
long sumOfSquares = 0;
for (int i=0; i<numberOfSamples; i++){
sample = analogRead(0);
signal = (sample - middleValue);
signal *= signal;
sumOfSquares += signal;
}
averageReading = sumOfSquares/numberOfSamples;
runningAverage=(((averagedOver -1 )*runningAverage)+averageReading)/averagedOver;
if(runningAverage>threshold){
digitalWrite(ledPin, HIGH);
}else{
digitalWrite(ledPin, LOW);
}
Serial.println(runningAverage);
}
When the arduino suddenly stopped sending serial numbers, I pressed reset button and uploaded the default code:
void setup() {
}
void loop() {
}
but now it shows that it cannot connect to COM3 (arduino) and cant find the device, When I can see arduino uno successfully connected to PC using device manager (windows 8.1). The led light of arduino also turns on when I connect it to power source or usb.
it shows "port not found" when I click to see the serial output
Did I just fry Arduino?
How should I fix this?
Also, i checked the led pin 13 blinking three times when i plug in the usb. I just cant upload anything
What worked for me:
Tools>Port>(your COM)
Just selecting that solved the error.
You should do a few checks in order to jump to a conclusion, your Arduino may be just fine. As far as I know, Code cannot destroy a controller.
Check if any other software is using the same serial port. Two softwares cannot use the same serial port at the same time
Restart your PC and then try again
Remove the Microcontroller from the board, connect it to the PC and try to open the com port. If it opens then connect your Tx pin with the Rx pin, send some data and check if you are getting the data back. This way you will ensure your USB-TTL converter is fine
If this goes successful, then insert your microcontroller in some other board and check if it is getting programmed
I am sure after these checks you will find out the reason of the failure of your board/microcontroller.
Yes, check everything.... especially the USB cable or the USB port. It is crucial. My problem was just dirt on the computer USB port. Dirt, dust and grime. Crazy isn't it ? Just clean up the USB port and connector and the problem solve.
I also had a same problem and solved it.
try this one.
1. disconnect all cables from your arduino
2. connect external power
3. connect usb cable
4. then upload it.
I used my arduino with CNC shield(GRBL) and plugged external power to arduino.
after this. it was not possible to upload new firmware to arduino with usb cable only.
Once you success to upload with usb + external power then you can upload any firmware with usb cable only.
To solve the problem.
Method-1:
Go to Tools>Port>Select the Port
Method-2:
Unplug your board and plug it back in.
Method-3:
Restart your computer or laptop and reinstall Arduino software.
For more details you can visit "https://arduinopoint.com/fix-most-common-error-uploading-to-arduino/"

Arduino: Incoming USB serial data when USB is disabled hoses the connection

I'm writing a sketch for an Arduino Leonardo that occasionally needs to communicate bidirectionally via USB serial, and also needs to be power efficient. To save power I'd like to disable the USB when it's not needed. I can successfully use power_usb_disable() and power_usb_enable() to do this, BUT only if no data is sent to the Arduino when USB is disabled.
Here's how to reproduce the problem: I run the following sketch on the Leonardo while it's connected to a serial comms program on my computer (I've tried the Arduino serial monitor and Minicom in Linux and PuTTY in Windows). It works as expected, printing out the messages, until I type something right after the "Disabling USB..." message. Once I do that, I will receive no further data from the Leo. The funny thing is, the TX and RX LEDs continue to blink as if everything is fine. The only way to get the connection working again is to reset the board.
Now, I don't really care about dropping the incoming data when USB is disabled, that's to be expected...but I would like the serial connection to continue to work correctly once USB is enabled again.
I don't know enough about how USB serial works to know how to troubleshoot this.
#include <avr/power.h>
void setup() {
Serial.begin(9600);
delay(3000);
}
void loop() {
Serial.println("USB is enabled!");
delay(1000);
// If I send characters here, everything is fine.
Serial.println("Disabling USB...");
delay(25);
power_usb_disable();
delay(1000);
// If I send characters here, it hoses the USB connection somehow.
power_usb_enable();
delay(25);
}
I guess for now the best answer to this problem is "don't do that." I can arrange not to disable the USB if the connection is open, using the "if (Serial)" thing. So if somebody has a serial monitor open and looks as if they're about to type something, I leave USB enabled. Seems to work OK, although it doesn't address the underlying general question of how to robustly disable and re-enable USB from any state it might be in. I would still love to hear expert advice on that.

Resources