element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Rate my arduino code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 26 replies
  • Subscribers 384 subscribers
  • Views 4293 views
  • Users 0 members are here
Related

Rate my arduino code

scottiebabe
scottiebabe over 2 years ago

I have never used an actual arduino board and have only tried the arduino framework on non-arduino hardware a few times.

I am putting together a little test box for an ADS1115 ADC module (or clone of  New clone on the block  ) 

Quite stuff inside the box, noisy stuff outside the diecast box, nothing fancy.

image

I am using a RPI Pico running the arduino framework using platformio

image

Here is the code I put together using Adafruits arduino library for the ADS1115

// Sample an ADS1115 ADC via i2c with a RPI Pico
// PICO GPIO20 - I2C_SDA
// PICO GPIO21 - I2C_SCL
// PICO GPIO16 - ADS1115_DRDY
//
// Inspired by Adafruit demo, use as you like.
// scottiebabe/sstobbe@2n3904blog.com

#include <Wire.h>
#include <Adafruit_ADS1X15.h>

// GPIO16 ISR
void ADSInterrupt();

Adafruit_ADS1115 ads1115;
MbedI2C i2c(20,21);  // Use GPIO Pins 20,21 on Pico for i2c

void setup(void)
{
  Serial.begin(9600);
  //while (!Serial);
  delay(1000);
  Serial.println("Hello!");
  Serial.println("Getting single-ended readings from AIN0..3");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 187.5uV)");
  ads1115.begin(0x48,&i2c);

  // Enable Pico's onboard LED
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN,1);

  // Setup ISR on GPIO16
  pinMode(16,INPUT_PULLUP);
  attachInterrupt(16, ADSInterrupt, FALLING );
  gpio_pull_up(16);

  Serial.println("Hello, pt2!");
  Serial.println("Configured ADS1115 for single channel continuous conversion.");
  Serial.println("ADC Range: +/- 256mV (1 bit = 7.8125 uV)");
  Serial.println("Fs: 860 Sa/s");
  Serial.println("Ain: Ain0-single ended");
  //ads1115.setGain(GAIN_TWOTHIRDS); // SET FSR +-6V
  ads1115.setGain(GAIN_SIXTEEN); // Set FSR +- 256 mV
  ads1115.setDataRate(RATE_ADS1115_860SPS);
  //ads1115.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1,true);
  Serial.println("Starting Acquisition! ...");
  ads1115.startADCReading(ADS1X15_REG_CONFIG_MUX_SINGLE_0,true);
}

#define ADS_BUF_LEN (4096)
volatile int16_t  samples[ADS_BUF_LEN] = {0};
volatile uint32_t tstamps[ADS_BUF_LEN] = {0};
volatile int adsidx = 0;
volatile int adsinterrupt = 0;

// GPIO16 ISR
void ADSInterrupt() {
  if( adsidx < ADS_BUF_LEN ){
    tstamps[adsidx] = time_us_32();
  }
  adsinterrupt = 1;
}

/* Adafruit Demo Loop
void loop(void)
{
  int16_t adc0, adc1, adc2, adc3;

  adc0 = ads1115.readADC_SingleEnded(0);
  adc1 = ads1115.readADC_SingleEnded(1);
  adc2 = ads1115.readADC_SingleEnded(2);
  adc3 = ads1115.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.print("AIN1: "); Serial.println(adc1);
  Serial.print("AIN2: "); Serial.println(adc2);
  Serial.print("AIN3: "); Serial.println(adc3);
  Serial.println(" ");
  
  delay(1000);
}
*/

void loop(void){

  if(adsinterrupt){
    if( adsidx < ADS_BUF_LEN ){
      samples[adsidx] = ads1115.getLastConversionResults();
      adsidx++;
    }
    adsinterrupt = 0;
  }

  if(adsidx >= ADS_BUF_LEN){
    //Print out the sample buffer to serial console
    Serial.println("Acquisition Buffer:");
    for(int i=0; i<ADS_BUF_LEN; i++){
      Serial.print(tstamps[i]);
      Serial.print(",");
      Serial.println(ads1115.computeVolts(samples[i]),7);
      delay(1);
    }

    
    // dummy read to re-arm interrupt
    ads1115.getLastConversionResults();
    adsidx = 0;
    adsinterrupt = 0;
    Serial.println("Starting Acquisition! ...");
  }
}

Thumbsup / Thumbsdown

  • Sign in to reply
  • Cancel

Top Replies

  • Jan Cumps
    Jan Cumps over 2 years ago +4
    I'd move the defines and global variables to the begin, under the includes. That's the only nitpick.
  • kmikemoo
    kmikemoo over 2 years ago +3
    Not that this will help any at all but... if it runs, it's always a .
  • scottiebabe
    scottiebabe over 2 years ago in reply to Jan Cumps +3
    Using this decorator puts my function in sram
  • kmikemoo
    kmikemoo over 2 years ago

    Not that this will help any at all but... if it runs, it's always a Thumbsup.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • wolfgangfriedrich
    wolfgangfriedrich over 2 years ago

    Serial.begin(9600);

    I never had issues even with Uno R3, running the serial terminal at 115200 baud.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • carlacda
    carlacda over 2 years ago

    Thanks for sharing your idea.

    be honest I wasn’t aware that there is an active support of Arduino framework for pico… I would like to try it!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 2 years ago

    That looks really good, and easy to read.

    very minor details, choices: For more clarity I might use a bool (boolean) for adsinterrupt, and it looks like you could skip reporting adsinterrupt when the ADS_BUF_LEN has been passed (ie, put the line that sets it inside of the if block).

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago

    Thumbsup

    I'd move the defines and global variables to the begin, under the includes. That's the only nitpick.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to kmikemoo

    Thanks for the word of encouragement. I was excited to see that it worked (or as best as I can tell), Arduino is pretty cool.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to wolfgangfriedrich

    I wasn't really sure about that, I am using the USB port on the pico for serial communication. I just copied what I saw in an adafruit example, I don't know if it actually gets used with USB CDC.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to carlacda

    Thank you for the kind words! In this case it was really easy and worked without issues. Its pretty neat. The pico is also low cost Slight smile

    I like it!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to ntewinkel

    The e14 beehive of knowledge always has great advice. 

    Thank you for the if logic advice, I should have paid more attention and not got so caught up in the excitement of a new project. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to Jan Cumps

    Thanks! I definitely agree. I could of maybe added a few more top line comments too about what its doing.

    I also found out the Pico doesn't have an input capture peripheral like most microcontrollers 

    image

    So I just copied the systicks counter value to a buffer in my ISR. 

    Re: ISRs on RP2040. Technically I guess I should have told gcc I want my ISR to run from SRAM for more predictable latency. But in the case of using the Arduino framework I'm not sure that matters since there is so much additional logic running before my ISR.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube