Go Down

Topic: Data transfer through ArduinoMega (Read 288 times) previous topic - next topic

imbatronics

Jan 21, 2020, 11:32 am Last Edit: Jan 21, 2020, 11:34 am by imbatronics
Hello!

I am an absolute beginner and would like some help and guidance with my project.
I have experience in programming back-end servers and websites, but not much in electronics.

I have a coffee machine I would like to alter, because some settings are weird.
For example, 1) default sugar setting is 3 spoons, I want to change it to 0.
2) Change order of making cappuccino. So instead of making espresso first, and pour milk over it,
I want the machine to pour milk first and espresso later.
3) Eliminate front screen with menu buttons and be able to remotely make coffee with my phone.

So there are 2 PCBs in the machine.
Front PCB is the menu board where it stores all data of each coffee selection
Back PCB is the controller board where it controls all motors, valves, etc.
As shown below, these 2 boards communicate in Rx Tx wires only.


I have been fiddling around and my first try was to try read analog Data, and immediately pass through arduino and write analog data to the PCB. But the machine is saying there is a comms error.
The wiring are as shown below.


The wiring is right, but there must be something I don't understand that cause the 'comms error'.
Could it be that the data is out of sync? Am I suppose to use analog read and write? Baudrate?

I'm clueless on how electronics really work, so please use beginner words :D

Any help or guidance would be greatly appreciated.




gcjr

Quote
I have been fiddling around and my first try was to try read analog Data, and immediately pass through arduino and write analog data to the PCB. But the machine is saying there is a comms error.
you should use pins 1&2 and Serial.   you'll need to verify the speed (e.g. 9600).   this assumes is it's conventional format with start, parity and stop bits.
greg - somerset, nj

TheMemberFormerlyKnownAsAWOL

You should also verify that the supposed serial comms are using TTL levels, and not inverted RS232.

Quote
So instead of making espresso first, and pour milk over it,
I want the machine to pour milk first and espresso later.
This is so wrong, I doubt your sanity.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

GolamMostafa

#3
Jan 21, 2020, 12:12 pm Last Edit: Jan 21, 2020, 12:14 pm by GolamMostafa
You are planning to build a coffee making machine; that is highly appreciated; there is nothing wrong in day dreaming; but, it requires tremendous efforts and patience to bring a bit of day dream into reality -- the night dream.

Do you have 2 Arduino MEGAs? If so, connect them together using UART1 Port (TX1/RX1 and GND of MEGA1/PCB1 <----> RX1/TX1 and GND of MEGA2/PCB2) and create sketch to exchange the following message between them.

MEGA1 sends "Hello MEGA2!" to MEGA2 which appears on Serial Monitor of MEGA2.

In response --

MEGA2 sends "I am here!" to MEGA1 which appears on Serial Monitor of MEGA1.

Now, you know that the MEGAs are working. You can use them to carry out the rest of the works task by task.

Please, post the codes of your program/sketch.


Robin2

#4
Jan 21, 2020, 12:45 pm Last Edit: Jan 21, 2020, 12:46 pm by Robin2
I have been fiddling around and my first try was to try read analog Data, and immediately pass through arduino and write analog data to the PCB.
Why do you think analog data might be useful? - there is no evidence for it in your diagrams. Serial data is digital.

This is not intended to be a derogatory comment. If we know your thought process it will make it much easier to help.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

imbatronics

#5
Jan 21, 2020, 01:03 pm Last Edit: Jan 21, 2020, 01:14 pm by imbatronics
you should use pins 1&2 and Serial.   you'll need to verify the speed (e.g. 9600).   this assumes is it's conventional format with start, parity and stop bits.
The manufacturer doesn't want to disclose their datasheet, so I have to find out or guess what the baudrate is... :( 

You should also verify that the supposed serial comms are using TTL levels, and not inverted RS232.
 This is so wrong, I doubt your sanity.
I used oscilloscope to check and when there isn't any data, the voltage was at 0v, and jumps to 5v when data is transferred.
Milk was a bad example, but my future plan is having milk dispensed first, robot arm takes it away and have it frothed, etc.

@GolamMostafa
It could be used for commercial development if people like it :D
I will try your method and reply with codes and schematics.
But is it a must that I use 2 arduino megas instead of 1?

Why do you think analog data might be useful? - there is no evidence for it in your diagrams. Serial data is digital.

This is not intended to be a derogatory comment. If we know your thought process it will make it much easier to help.

...R
I don't even know why I used analog haha, I guess I will change it to digital since others suggest too. I have very limited micro-controller experience so please bare with me.


Thank you all for the replies, I will try to make it work by using digital input and output, and try all the suggestions and reply back.

Could someone help me with finding out the baudrate?
I don't have an oscilloscope, but I have tried using arduino oscilloscope which is not perfect but can somehow give some information.

imbatronics

#6
Jan 21, 2020, 01:59 pm Last Edit: Jan 21, 2020, 02:00 pm by imbatronics
I got something working.
I used Tx Rx as suggested and it worked! Once!
Only worked once but its something.

If its working correctly, the coffee machine screen displays as follows.

If theres error, screen displays as follows.

When I disconnect 1 Rx wire from coffee machine, it displays 'comms error'
Then when I wire it in, like the diagram below,

It displays correct screen message for 1 or 2 seconds, then goes back to 'comms error'.

So now I'm assuming that the 'sync' or baudrate is off.
Or my code is wrong?

Code: [Select]

int data = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  // send data only when you receive data:
  if (Serial.available() > 0)
    {
    data = Serial.read();
    Serial.write(data);
   }
}

Robin2

As you have a Mega why not make use of the fact that it has 4 HardwareSerial ports. Use Serial1 to connect to one of the devices and use Serial2 to connect to the other one. Then your code would like this
Code: [Select]
  if (Serial1.available() > 0)
    {
    data = Serial1.read();
    Serial2.write(data);
   }


And you can still use Serial separately for messages to your PC while developing the program.

Trying to connect two serial devices to the same wires is likely to confuse both of them.

It may also be the case that you need to read data from Serial2 and send it to Serial1. Many communication systems rely on two-way communication. The error you see may arise because a response was expected and never came because the request for the response was not passed on.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

imbatronics

As you have a Mega why not make use of the fact that it has 4 HardwareSerial ports. Use Serial1 to connect to one of the devices and use Serial2 to connect to the other one. Then your code would like this
Code: [Select]
  if (Serial1.available() > 0)
    {
    data = Serial1.read();
    Serial2.write(data);
   }


And you can still use Serial separately for messages to your PC while developing the program.

Trying to connect two serial devices to the same wires is likely to confuse both of them.

It may also be the case that you need to read data from Serial2 and send it to Serial1. Many communication systems rely on two-way communication. The error you see may arise because a response was expected and never came because the request for the response was not passed on.

...R
Hi Robin.
Thank you so much for the reply.

I have tried your method of having 2 Serial ports open, and the result is same as before. It works once only.

I have used oscilloscope to see what happens in successful comms and there are 2 things that change.
The frequency of the 'poll' pulse (identical data that repeats it self) doubles.
There are additional different data, which I assume its an acknowledge data.

So I totally agree with you that my coffee machine uses 2-way communication.


Now how will I go about achieving 2-way communication?
At the moment, it is wired as you suggested, 1 wire to Rx1 and 1 wire to Tx2.

Can 2-way communication be achieved with software? or do I need extra hardware to achieve this?

imbatronics

Or could the reason be because of TTL, RS232,etc etc?

When I use oscilloscope on coffee machine wire, the resting voltage is 0V.

I've learn't that the arduino's Tx resting voltage is 5v.
If I connect the arduino's Rx and Tx to the device, do I need to 'flip' the voltage(or use an adapter)?

imbatronics

#10
Jan 21, 2020, 05:30 pm Last Edit: Jan 21, 2020, 05:34 pm by imbatronics
I managed to find a pattern.

When I connect 1 wire to Rx and have a osciloscope monitor the Tx pin, there was a pattern.
I had to adjust the baudrate to get a similar pattern. The baudrate was 13000.


How do I make the Tx reading to be exactly same as the Rx readings?
How do I 'flip' the initial voltage or have the Tx resting voltage as 0v?


Robin2

So I totally agree with you that my coffee machine uses 2-way communication.


Now how will I go about achieving 2-way communication?
At the moment, it is wired as you suggested, 1 wire to Rx1 and 1 wire to Tx2.

Can 2-way communication be achieved with software? or do I need extra hardware to achieve this?
Slow down. Let's try and deal with one thing at a time. Let's start with this one.

Please post the program you are using with the two serial ports and also (very important) a diagram showing how everything is connected. Please DO NOT have any of those plastic breadboard pictures in your diagram. Just draw it with pencil and paper and label all the connections. Then post a photo of the drawing.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

imbatronics

#12
Jan 21, 2020, 09:33 pm Last Edit: Jan 21, 2020, 09:34 pm by imbatronics
Hi Robin

Here are the diagram. Please let me know if this makes sense.

The original schematic of coffee machine

The current modification

Pictures of machine and setup



Robin2

In your diagram marked "current modification" you have the bottom Rx and Tx bypassing the Mega. I had been assuming that they were also connected to the Mega serial ports.

In theory, the way you have things the BACK can talk to the FRONT directly so a 2-way conversation could take place. However I have no idea whether it matters that there is a small delay in the F to B direction due to the Mega intercepting the signal and there is no corresponding delay on the B to F communication.

If you connect the bottom Rx and Tx through the Mega you should be able to monitor the conversation (if any) from B to F.

For the future, photos of hardware are rarely useful.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

imbatronics

#14
Jan 22, 2020, 12:59 pm Last Edit: Jan 22, 2020, 01:00 pm by imbatronics
Hi Robin

Thank you so much for the help!

I used the oscilloscope to check if there are 2-way communication happening.
So there are Tx and Rx on both F and B PCB boards and after probing, I found out that only Tx sends out data and nothing at all from Rx. So Tx from F board is sending data and Tx from B board is sending data.

I have been fiddling around doing everything I can.

I have tried other method and it is working!
I have ditched the arduino's serial comms as it was using TTL method.
Instead, I used the digital pins of arduino and voila! It is working!





The code:
Code: [Select]

int receive1 = 2;
int send1 = 3;
int receive2 = 4;
int send2 = 5;
int test = 6;

void setup()
{
  Serial.begin(9600);
  //Works with any baudrate
  pinMode(receive1, INPUT);
  pinMode(send1, OUTPUT);
  pinMode(receive2, INPUT);
  pinMode(send2, OUTPUT);
  pinMode(test, OUTPUT);
}

void loop()
{
  int data1 = digitalRead(receive1);
  int data2 = digitalRead(receive2);
  digitalWrite( send1, data1);
  digitalWrite( send2, data2);  
  //digitalWrite( test, data2);  it distrups timing
  //Serial.println(data2); it distrups timing
}

At the moment, lacking knowledge of how things are working, if I add other lines of code or command, it messes up and the coffee machine displays 'comms error' message.
So I assume data needs to be sent back and forth in precise timing.

So my goal is to eliminate the FrontPCB and replace it with the arduino.
To do so, I think I must
1) Replicate what FrontPCB's Tx is sending.
2) Send F's Tx to B's Rx with precise timing
3) Capture other commands and observe what and how F and B sends commands in which protocol, etc.

How will I be able to achieve reading data coming in and out without disrupting the flow?


Go Up