Thursday, 20 July 2017

Intro to using Arduino


 I've just been given a Biomaker grant, which came with a lot of Arduino hardware as a kit. I've not used Arduino before so I'm exploring the documentation online and noting which bits are good, where there are areas that are tougher to find out about. I wanted to note what I'm finding here in case it helps others. So here's what I've found:


Basic Introduction


 Arduino Microcontroller guide is a good introduction to the electronics of the Arduino. It also covers the basics of computer programming, and the syntax of the Arduino language.


Arduino shields is a good basic introduction to using shields.

Keep it simple


Initially I thought it would be great to use lots of the hardware that I've been given, but the complexities quickly spiralled out of control, as it's often hard to find good code and the docs that I need to make different bits of kit work together. In the end I've found that it's best to aim for the most minimalist setup that I can think of that still gets the job done. That way I can get everything to work together and not have inexplicable bugs.

Discussion Forum


 There is a dedicated Arduino Discussion Forum, where we can take technical questions. Arduino The Arduino itself was very clearly explained in the introductory docs mentioned above.

ARDX


This kit is easy to use. There are small maps in the box, and if I copy the wiring diagrams on them and use the downloaded code then they just work, really easily.

Grove 


 This kit is more difficult to use. The docs on the Seeed site are lagging somewhat behind the really great hardware. To get better help, I tried searching for the name of the individual component (e.g. light sensor) on mouser.com to find useful docs like this or just searching for the name of the component with “Arduino”.

Sidekick


I haven't used this yet.

 4D Display


I am not using this as I don't get on with LED displays.


 Gtronics Proto Shield Plus

I haven't used this either, although it is extremely cool looking.

Stacking shields

If stacking more than one shield on top of an arduino, I have learned that it’s important to check that the shields are not using the same pins on the arduino to send and receive messages. Otherwise the messages will get muddled up. Below are pages that list which pins are used by which shields:
Arduino page
...

Which pins does the Grove Base shield use?

As far as I can see, this depends on which plugs on the shield we are using. For example if we are using the plug that says I2C then we are only using pins GND, VCC, SDA and SCL which are the labels written on the board next to the wires on that plug. GND, SDA and SCL are all places that pins can go in the black bars at the side of the Arduino. VCC is a label on the switch that changes the voltage.

Further information is given on the Grove page that goes with this specific version of the kit. For example this page says that on the I2C ports: “Below the digital ports are four I2C Grove ports. I2C is a low-speed bus protocol that transfers data via two wires: SCL and SDA. SCL is the clock line for synchronizing data transfer over the I2C bus; SDA is the data line.”

Installing the IDE (programming environment)

The Arduino IDE is a free download. It is simple and works well. It doesn't give much help on bug fixing, which is tricky with longer programs. I tried more complex software tools but none were easy to set up, so I have decided to just keep my code really simple and bug fix by eye.

Code syntax checking IDE

I tried out Visual Studio and PlatformIO IDE, but couldn’t get it to work. I also tried tried Atom with PlatformIO-IDE but it does not support debugging with Arduino Uno. Visual Studio did not even list Arduino Uno as an option. I also tried Eclipse but it turned out that the set up of the Arduino plugin was not well enough documented and ended up being impossible to get right.

Including libraries

Some things in the kit, like the LCD screen, need libraries to be installed. The Arduino site gives help on how to install new libraries that are not yet in the programming environment (IDE).
Once the library is installed, if you want to include it in your sketch (computer program) then you can just do this:









In the case here I would be choosing the library that supports the Ethernet shield and choosing this menu option just adds this piece of code to the top of my sketch:



After learning this stuff above, I have been able to get on and just make things work pretty well, and that was just a week of learning so that's not bad really.

Optimizing light sensor



This page leads on from the first development of the light sensor.

I have now stripped the system down to just the Arduino, the Grove Base Shield, the light sensor, the LCD and a battery to make the unit portable. However the sensitivity of the light sensor is not at all in the range that I need it. I detects from indoor light down to darkness well, but anything above indoor light intensity is off the scale. I need it to distinguish up from room light to the brightest outside summer light.

This is the code I am using:

#include <Wire.h>
#include <rgb_lcd.h>
rgb_lcd lcd;

//Set lcd backlight to off.
const int colorR = 0;
const int colorG = 0;
const int colorB = 0;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
// Print a message to the LCD.
lcd.print("Light intensity");
delay(1000);
}

void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
// For LCD: set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(sensorValue);
delay(100);
}

To improve the range of light sensitivity I have ordered a better light sensor called a DFRobot Light Sensor-BH1750.

Hopefully that will get me more useful data.

-----------------------------------------------

The new BH1750 light intensity sensor has arrived:




Image from https://www.dfrobot.com


Here is the wiring diagram from https://www.dfrobot.com:





This is a schematic of the wiring from http://www.esp8266learning.com/bh1750fvi-ambient-light-sensor-example.php.





I tried the code from several different webpages but they did not work. Finally I found that the code from this page works: http://www.esp8266learning.com/bh1750fvi-ambient-light-sensor-example.php

This is the working code:

#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup(){
Serial.begin(9600);
lightMeter.begin();
Serial.println("Running...");
}

void loop() {
uint16_t lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
delay(1000);
}

The important thing seems to be the library that comes with it, which is called BH1750-master. The original version of this library called BH1750 doesn't work for me.

The sensor works and seems to have enough range to measure from darkness up to bright sunlight. Bonus. Now I just need to get data on which numbers represent which brightnesses and document it meaningfully. I also need to add the screen in and the battery so it becomes a hand-held meter.


This is continued at Working light sensor.










Wednesday, 19 July 2017

Working light sensor


I have managed to get my light sensor working now, and plotting light intensity on the computer screen. Below is a video of the light sensor working. When I hold a card reader over the light sensor to put a shadow over it, the graph of light intensity shows that the drop in light intensity is detected.


This is the hardware setup:


It is very simple. It is just an Arduino computer with a Grove Base shield on top. The grove light sensor is plugged into the A0 port of the base shield.


Here is a photo of the code and the monitor:


Below is the code that I used. It came from a DIY Hacking page by Arvind Sanjeev.


int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(100);
}


--------------------------------------------

The next thing that I would like to do is to include an ethernet shield on top so that I can store the Arduino on a remote location with the light sensor outside, and so that the light information will be fed back to my PC like a weather station. I'm going to start by trying to write the values as comma-delimited text to the SD card on the ethernet shield. 

I'm taking the code that I have, and trying to incorporate the additional commands from the Arduino page on writing to SD. I am also following the Arduino instructions on concatenating strings. 




This is the code I have so far and it compiles and runs. It writes the numerical data on light intensity to the SD card. 



#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600); //sets serial port for communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}
void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  dataString += ", ";
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  dataString += sensorValue;


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  delay(100);
}


--------------------------------------------

The next thing I have done it to make the Arduino write the light intensity value to the LCD so that the system can be used as a hand-held light meter in the garden.

Here is a video of it working:


I got the code for the LCD from the Seeed-Studio section of GitHub


Here is my current total program:




#include <SPI.h>
#include <SD.h>


#include <Wire.h>
#include "rgb_lcd.h"

rgb_lcd lcd;                                           

const int colorR = 0;
const int colorG = 0;
const int colorB = 0;

const int chipSelect = 4;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600); //sets serial port for communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // set up the LCD's number of columns and rows:
      lcd.begin(16, 2);
     
     lcd.setRGB(colorR, colorG, colorB);
     
      // Print a message to the LCD.
      lcd.print("Light intensity is");
  
     delay(1000);
}
void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  dataString += sensorValue;


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }

  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  delay(100);
  
     // For LCD: set the cursor to column 0, line 1
     // (note: line 1 is the second row, since counting begins with 0):
     lcd.setCursor(0, 1);
     // print the number of seconds since reset:
     lcd.print(dataString);

     delay(100);
}


Here is the whole thing working together. No idea why the camera can't focus when taking video.




I now need to test whether the light sensor has enough range to measure everything from darkness to the brightest sunlight. It seems to max out in bright torch light which is a bit odd. I thought it would have something above that.

I did some tests and the range didn't seem to be sufficient so I am going to strip out all unnecessary stuff and see if I can increase the range.

This is continued at Optimizing light sensor.


Thursday, 13 July 2017

The beginnings of a light meter.


I am trying to build a light meter just now using an Arduino and a Seeed Grove starter kit

The starter kit comes with a light sensor and today I managed to figure out how to get some results from it. I used the docs from mouser.com.


Light sensor:


LED output



Grove shield



Grove shield on top of Arduino





I used the program from pdf above, and the readout from the light sensor came out on the serial monitor (below). The physical set up was as shown in 4.1.1 in the pdf,



My hope is to wire the arduino up to an LCD screen and a switch so that I can switch the machine between displaying current light intensity and cumulative light exposure since the machine was turned on.

This is useful for two things:

1) Comparing gardening conditions with distant friends over facebook.

2) Figuring out how long I need to be out in the garden over winter in order to get enough sunlight. This varies depending on the weather and the time of year and the geographical location.

Anyway.. I'll keep at it.



---------------------------------------------------------




In other news, a mystery guest has been soldering pins onto a Raspberry Pi Zero in our garden. :-)



Saturday, 8 July 2017

ARDX starter kit for Arduino.

We have just been given a Biomaker grant from the University of Cambridge and we are working up our Arduino skills.

I kicked off the fun with a bit of wire art.



This is all the fun equipment that we were given:



 We started with the ARDX starter kit for Arduino, and tried building this set that comes with instructions in the kit.

 

Here it is working:



This is what the code looks like in the arduino software on my laptop. The laptop is connected to the arduino by USB cable. Tim did this part. 



I also tried the gen4-uLCD-32DT touch screen and it powered up nicely when attached to my laptop. The docs said that the next step is to pay $80 for the IDE so I put that to one side for now. LED backlit screens nuke my eyes really badly, so I don't think I'll be braving this piece of equipment much, in spite of my son urging me to help him build a smartphone. 



The I tried the gGrove Arduino starter kit and it was very interesting. It has a light meter which I'm keen to try. 



The enclosed instructions were not sufficient to get started but it had a very cheery paragraph directing me to some online help material.


The online help page was unfortunately blank. 


I have put that kit to one side for now too. I figure I will be able to work it out, but it might take a bit longer without clear instructions. 

Later: It turns out that the docs are here.

Onward and upward...