Clarity on General Concept of Arduino Programming

I'm sure these are probably rookie questions but before I get too far into my project, I wanted to make sure I have the concept correct about my Arduino Nano 3.0 (8 analog inputs) and Raspberry Pi 2 B+. These are my questions:

  • There is only one script file that loads and executes on the Arduino
  • The best method of communication between the two is through USB serial
  • The best method to get multiple sensor results is returned as a list or array through serial communication in the main loop to be parsed on the RPi?
  • And following up on the multiple sensor results: Arduino will have 4 different sensors connected: 2 load sensors, 1 EC sensor and 1 pH sensor or basically 4 voltage dividers. EC and pH sensor can't be on at the same time but load sensors can be on all of the time. For the RPi to get the results of the EC sensor or pH sensor (that can't be on at the same time), is the best method to send a command to serial that tells the loop to turn on/off each sensor and return results?
    .

what heavy job do you have for the Pi that can't be done by Nano?

  1. There is only one script file that loads and executes on the Arduino

Correct. There is no operating system so it just runs one program. Although there are ways of making it "look like" it's multitasking, or you can have a menu to choose between different operations/functions so it "looks like" you're choosing between different programs, etc.

  1. The best method of communication between the two is through USB serial

I'd say that's the "easiest" way.

...I've never used a Raspberry Pi.

Why can’t the EC and the PH sensors be on at the same time?

If they really can’t then either the Arduino checks their values in turn an saves the results for sending to the RPi in the next message or the RPi can send a message to the Arduino telling it what to do next. This is really a matter of system design rather than writing code.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

All of this concept can be used in any programming language so it is as valid for receiving the data on the RPi as on the Nano.

This Python - Arduino demo may also be of interest.

…R

This sounds like chemical engineering! :grin:

I'm assuming the pH and conductivity sensors interfere with each other if they are in the same solution, and that the force sensors determine the mass of 2 vessels (that maybe you are mixing?) And the Pi runs a graphical UI?

Arduino programs (they are not, in a technical sense, scripts) can include different parts, and are written in C++ coupled with an Arduino library that provides descriptive function names for interacting with Arduino's hardware. They are usually clock driven programs, that run repeatedly. It's a pretty neat system for controlling machinery that does the same kind of thing over and over.

Sending a serial string is one way of transmitting data. rPi also has a bit of gpio capability, so perhaps a more complex interface would give the best performance. It might be best to have the Arduino turn on and off the electrical sensors, and hold the two data until you are ready to send it to the rPi. In fact, a lot of data conditioning can be done on the Arduino, which would simplify your Pi code. Reading the EC & pH takes a couple analog to digital conversions, so the data generation should complete in a quarter of a millisecond, give or take, plus however long to read the sensors for.

Juraj:
The raspberry pi runs a database that replicates to a server database, it records video through the raspicam, it runs a web server and interacts with a server RPi as well as other client RPis in the cluster (and I'm sure a few other things that I forgot). It also allows me to SSH in and do the development work directly in the environment.

Robin2: Thank you for the links, I will check them out.

ChrisTenone: You're correct, the EC sensor uses a solution as the resistor to test electrical conductivity which would throw off the pH sensor in the solution. They have to tested separately because it is using DC. The load sensors are for measuring mass on a different part of the project. I plan on using RPi to send instructions to the Arduino on which sensor should be turned on and for how long, then processing the results when received. The RPi won't run a GUI when deployed but I need it for development. But overall, this is a chemistry project, yes. Initially I was using an ADC breakout board for the RPi but accuracy is a problem and Arduino costs about the same as ADC.

Reading the sensors takes a few seconds to let them level out and ditch the first couple of readings. So my understanding is correct. My plan is to code the Arduino to return an array with the two load sensors first, then EC, then pH but until instructions are received via serial from RPi, the EC and pH sensor will remain off and return 0. The RPi operates on a schedule and when its time to record the EC or pH, it will send instructions to Arduino to turn the EC or pH sensor on and put the results into the return array. When the results in the array are greater than 0, that data will be stored in the database.

Thank you all for your insight.