Trying to learn how to use Arduino libraries - but a little help is needed.

Dear all,

I have been perusing the forum over many months, trying to use others’ examples to help me understand more about programming the Arduino. Unfortunately the examples I read seem to go too deep before I grasp the main core.

For example: I wish to read several temperatures, and act upon them. Initially I used an 8 channel multiplexer with thermistor inputs to a single analogue input on a Nano. It worked OK, and I could follow the sample code well enough to modify it for my specific project.
Then I was introduced to the Dallas DS18B20, and was instantly converted by virtue of its superior accuracy and the OneWire bus system which both eliminated the limitations and resolution of the analogue inputs, and enabled multiple devices to be read with just one digital input - FANTASTIC!

My problem is that I cannot seem to follow the sample software, and therefore I cannot confidently modify it for my purposes. For example, I started with the code as below:-

/********************************************************************/
// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2 
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
/********************************************************************/ 
void setup(void) 
{ 
 // start serial port 
 Serial.begin(9600); 
 Serial.println("Dallas Temperature IC Control Library Demo"); 
 // Start up the library 
 sensors.begin(); 
} 
void loop(void) 
{ 
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.println("Temperature is: ");
 Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire
   Serial.println(sensors.getTempCByIndex(1));
   Serial.println(sensors.getTempCByIndex(2));
   Serial.println();
   delay(5000); 
}

The libraries are declared:

// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>

I can follow this, but when it comes to

sensors.begin();

followed by

sensors.requestTemperatures(); // Send the command to get temperature readings

I get lost.

The pressing questions are 1. How do I find out which library contains the code from where ‘sensors.something’ comes? Easy when there are two libraries included, but when there are ten?
2. How do I find out what functions are available from within the library, functions which may not be used within the sample code I am trying to learn from, but useful within my specific sketch?
Previously I have been referred to the [name].h file and [name].cpp files on github (at least that’s where my enquiries seem to lead), but I have quickly been overwhelmed by the ‘stuff’, sometimes forgetting why and how I got there in the first place!!!

So I suppose that what I am asking is ‘Please will someone point me in the right direction to allow a structured approach to learning how to make the Arduino do what I want’; when I say that, it’s not that I don’t understand blink, or delay(), or pin assignment, more that I have hit the wall when it comes to understanding libraries, their use, and what they can do for my project(s).

Regards, GM

PS, I have just spent a couple of wasted hours trying to get Fritzing to sketch my project in an attempt to illustrate my latest project - without success (no luck finding a CT symbol on there!), so here is, for those who are interested, a brief outline of the project:

  1. Measure ac current up to 50A. (Done that)
  2. Measure several temperatures around an electric central heating system (9kW electric boiler to radiators). This will be where the DS18B20 sensors come in. Hardware safety devices, such as overheat cut-out, have been incorporated, but excluded from this enquiry for simplicity.
  3. Control the power to the boiler (which has 3 off 3kW elements) via two relays and one solid state relay, to give fully modulated contol between 0 and 9kW. I have the control strategy done, proportional control only will suffice, it just needs coding up.
  4. Limit the power to the boiler elements so that the total power consumption never exceeds 10kW. Clamp the control output?
  5. Display relevant data on a 20 x 4 LCD display. (I2C to be learned)

Now I know how to do many of the elements, though maybe without the elegance others’ may incorporate, but that will come with time and with knowledge…just clearing some of the fog around libraries will do for now!

For some this may appear to be a rant, and a long one at that; I assure you it’s not, but there is an element of frustration. If I can just, with your help and guidance, get past that…GM

Glorymill:
The pressing questions are 1. How do I find out which library contains the code from where 'sensors.something' comes? Easy when there are two libraries included, but when there are ten?

You're talking about where objects (mainly classes) are defined. Usually the object's name will give you a hint as to which library it comes from. For example, for your example the line to look at is:

DallasTemperature sensors(&oneWire);

This alludes to "sensors" being a class of type "DallasTemperature". The name "DallasTemperature" then alludes to it's definition being in "DallasTemperature.h".

Usually, if you're having trouble pinpointing the definition of an object, you can generally access the library repos in GitHub, where you can search all the files of the entire lib repo to find the definition.

Glorymill:
2. How do I find out what functions are available from within the library, functions which may not be used within the sample code I am trying to learn from, but useful within my specific sketch?
Previously I have been referred to the [name].h file and [name].cpp files on github (at least that's where my enquiries seem to lead), but I have quickly been overwhelmed by the 'stuff', sometimes forgetting why and how I got there in the first place!!!

The header (.h) files are where you need to go to determine the library's API and generally have all the info you need about the lib. You only need to look at the source (.cpp) files if you're curious as to how exactly the lib works. I've used many libs and rarely have ever needed to look at source files in depth at all.

Sometimes, however, you might get lucky and the library's README file is super in-depth or there might be an associated webpage that explains all about how to use the library.

Glorymill:
So I suppose that what I am asking is 'Please will someone point me in the right direction to allow a structured approach to learning how to make the Arduino do what I want'; when I say that, it's not that I don't understand blink, or delay(), or pin assignment, more that I have hit the wall when it comes to understanding libraries, their use, and what they can do for my project(s).

Honestly, learning libraries comes down to doing research on what libraries are available/useful for your specific purpose, and tutorials on how to use it. Look at the header as a last resort.

Once you know how to use a library, they can become powerful software tools to accelerate your project!

Power_Broker:
Usually, if you're having trouble pinpointing the definition of an object, you can generally access the library repos in GitHub, where you can search all the files of the entire lib repo to find the definition.

If the examples compile, you have already downloaded the libraries so it is easier to search them on your local PC than gitbub. On Windows.... ..\Documents\Arduino\Libraries...

Glorymill:
PS, I have just spent a couple of wasted hours trying to get Fritzing to sketch my project in an attempt to illustrate my latest project - without success (no luck finding a CT symbol on there!), so here is, for those who are interested, a brief outline of the project:

PLEASE, NO fritzing pretty pictures that are useless as a schematic. Just draw it on a piece of paper and scan it or take a picture.

blh64:
If the examples compile, you have already downloaded the libraries so it is easier to search them on your local PC than gitbub. On Windows.... ..\Documents\Arduino\Libraries...

Not necessarily, GitHub has a nice search feature that allows you to search for class/object definitions that come in handy when many header files and sub-libraries are involved.

Your project appears to be quite ambitious. To start, get comfortable with the IDE. Buy an Uno and a sensor kit, then start working with the example projects in the IDE. You’ll learn something with each of them.

Libraries- I have been writing project software using the Arduino IDE and many different board for five years. I am certainly no expert, but I am finally learning what to search for. It is extraordinarily rare that I need to look for answers in the library files.

I have also used the DS18b20 on a lot of my projects and I have never used the DallasTemperature library. Why don’t you start with the DS18x20_Temperature.ino example in the IDE (in the OneWire library)?

Thanks to all for giving their time and effort to help me on my way.
A little at a time, I suppose.

Luckily I have a little more time than expected at the moment...but strangely enough more other stuff with which to fill that time, and trying to get to grips with the Arduino language (is it called code now?) in half-hour chunks is not condusive to that knowledge sinking in to the old (in this case really old) grey matter.

:slight_smile: