matthewd139
Posts: 11
Joined: Sat Jul 02, 2016 9:12 am

Converting Arduino to Raspberry Pi

Fri Sep 16, 2016 11:45 am

I created an amplification circuit that uses an Arduino program to log the signals from several strain gauges.
It didn't have enough processing power so I am switching to a Pi and using external ADC chips.

I was wondering if it was possible to find out how to write some terms in Python format

Code: Select all

#include <SD.h>   //Includes the SD library

//=== Governing test conditions ===//

const char textFile [] = "Data.txt"; //Defines the file name for the data

const byte timeOfTest = 3;  //Sets the length of time that data will be written for in minutes

const byte testDataReadingInterval = 150;   //Sets interval between readings in microseconds

//=== Define variables used with the SD card ===//

const byte bodyGaugePin[] = {A0, A1, A2, A3, A4, A5, A6, A7};       //Defines the analog inputs
const byte legGaugePin[] = {A8, A9, A10, A11, A12, A13, A14, A15};  //Defines the analog inputs

int SdCsPin = 53;  //SD card chip select pin

//=== Initial setup ===//

void setup()
{
  Serial.begin(115200);   //Initialises Serial bus connection with 115200 a Buad rate
  
  //This tells Arduino that all input signals can move over a range of 1.1 V; this will be written
  analogReference(INTERNAL1V1); //in 10-bit resolution (2^10 = 1024; [1.1 / 1024 = 1 mV per bit])

  Serial.println("Initialising Card");

  //Chip Select Pin is an output
  pinMode(SdCsPin, OUTPUT);

  if (!SD.begin(SdCsPin)) // This states that if the SD card function has not started, then print text
  {
    Serial.println("Card Failure");
    Serial.println("Check SD card is inserted");
    while (!Serial.available()) {}
  }
  Serial.println("Card Ready");

  Serial.println();
  Serial.println("Begin writing test data");
  Serial.print("Data will be logged for ");
  Serial.print(timeOfTest);
  Serial.println(" minutes");
}

//=== Writes test data to SD card ===//

void WriteTestData() {  
  // Converts the digital bit reading back into mV for body gauges
  int BG_V1 = ((4.96 / 1024) * (analogRead(bodyGaugePin[0]) + 1.0)) * 1000;
  int BG_V8 = ... // This was previously used, but the ADC chips will write to the digital pins now

  // Converts the digital bit reading back into mV for leg gauges
  int LG_V1 = ((4.96 / 1024) * (analogRead(legGaugePin[0]) + 1.0)) * 1000;
  int LG_V8 = ...

  File testData = SD.open(textFile, FILE_WRITE); // Opens the text file to write data
  if (testData) {
    testData.print(abs(BG_V1));   // This determines which inputs will be logged
    testData.print("     ");      //
    testData.println(abs(BG_V8));
    testData.close();
  }
  else {
    Serial.println("Cannot write data to SD card"); //If data is unable to write, an error will appear
    while (!Serial.available()) {}
  }
}

//=== Main program loop ===//

void loop() {
  unsigned long time = micros();

  if (time <= timeOfTest * 60000000) { // Sets how long test will run for in microseconds, 60000000 = 60 seconds
    WriteTestData();
  }
  else {
    Serial.println();
    Serial.println("End of data collection");
    Serial.println("Press upload to collect more data");
    while (1) {}
  }

  delayMicroseconds(testDataReadingInterval);       // time between readings
}
The important points I want to know are:

Do I still need to define variables using: const, char, byte, int?

How do I use the clock to set up an interval or a delay? I would like to write the data every 150 - 200 microseconds.

How to select the digital pins

How to write to the SPI register




Thanks for any help

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Converting Arduino to Raspberry Pi

Fri Sep 16, 2016 12:13 pm

In python you do not need to specify variable types.
Python has the time library and specifically time.sleep(1000) for a 1 second delay
Easiest way to control digital pins is using GPIO Zero http://gpiozero.readthedocs.io/en/v1.3.1/
GPIO Zero also contains SPI code for reading from common external ADCs
If you want to store the results, then you won't need to use the SD commands like you did in Arduino. You can simply write a file, https://docs.python.org/3/tutorial/inputoutput.html
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

mjpelletier5325
Posts: 3
Joined: Mon Sep 05, 2016 6:30 pm

Re: Converting Arduino to Raspberry Pi

Tue Sep 20, 2016 4:03 pm

Not sure if this will help or not, since you are looking for Python code, but I covered some of these issues, in C, with a new data acquisition board I am developing. I did all my Raspberry Pi coding in Raspbian using only the text editor and the GCC compiler. The source code can be downloaded from my website, www.adaptint.com . Just go to the New Products page for the downloads.

The code covers accessing the BCM2836 registers to set up the SPI and GPIO using memory mapping and sending a 4-byte SPI packet to the data acquisition board. The board, called a DAAC-100, is sort of an MCP3008 on steroids. It has 8 12-Bit ADCs (sampled every 4 ms and stored as raw, filtered or as 32-sample running averaged), 3 12-bit DACs, plus PWMs, GPIOs and a digitally tuned oscillator. Right now, I just have prototypes but the board might help with some of your engineering projects.

Good Luck

Mark

Return to “Advanced users”