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...

Monday, 17 April 2017

Redrobotics Robot Testing


Redrobotics Robots

We've had a fantastic weekend getting to know some robots from http://redrobotics.co.uk/

My son is a big fan of these robots from seeing them at Pi Wars meetings, so Neil Lambeth, who designs and builds the robots kindly allowed us to borrow a couple to play with, photograph and write about. Here is what we found out. 

Firstly, by way of introduction, the robots are like remote controlled cars, but they can also grab and fire a ping pong ball, which means that they can be used to play football against one another. 

The robots are still in prototype form, especially the circuit boards. There's a lot of hand reworked components on them where the design is still evolving. We are really chuffed to be meeting these robots at such an early stage, and seeing how they change and improve over time. 

These are the two that came to us, along with their controllers:


The driving mechanism is in here under the hatch:


They take 8 x AA batteries each, and the controllers take 2 x AA batteries each and the batteries lasted several hours for us. The robots have to be powered on with a button in the lid, and then the controllers are turned on, and automatically connect, via a mechanism which I assume is bluetooth. My son, who is 6 years old, was able to look after his robot entirely by himself, as the steps are so simple and easy to carry out. 

The robots fire a ping pong ball from the front and they have an automatic grab feature, so that if the ball comes into the opening at the front then it is automatically gathered up and made ready to fire again.

The robots are simple to control. Although there are a lot of buttons on the controllers, the controls are just forward/backward, right/left, fire, double-speed, and soft fire. 

 Here is the blue robot firing a ball:



The main thing we found is that these robots are fantastic ice breakers for groups of people. 

When the robots arrived, I invited lots of friends from our local neighbourhood to come and visit. Some of these friends are regulars who come round a lot, and some were people who have never visited before. 

Over the weekend we had such a lot of fun with so many people coming to visit. They were all fascinated to meet a raspberry pi-based robot. All of them had a whale of a time, zooming the robots round our hall and firing the ping pong balls at goals. 

Here are a couple of our players in full swing:



We tested the fun with people aged 4, 6, 10, 12, 30+ and 40+ and all had a great time with them. The best thing was that everyone was having so much fun that we all forgot to feel self-conscious and just all had a great time. Even the non-talkers amongst us talked a lot. 

We've tried quite a few robots in our house (massive understatement) and these are by far the most fun as a social tool, and the best for bringing people together. 

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

So what goes on inside the robots?

The neatest part from my point of view was opening up the robots and seeing the components inside. The mechanism is based on the raspberry pi, but there is also a custom designed and built card on top. 

This is the whole mechanism as viewed with the hatch open. This picture and the ones that follow are worth clicking on to see them enlarged as the detail is really interesting. 










If there's one extra thing I'd love to see from this company, it would be a little map, explaining what the bits are. They look really fascinating, and I have literally no idea what most of them do, in spite of being a robot enthusiast and computer programmer. 

This stuff is the main reason why I would buy a robot like this, especially as it gives my son and his friends the chance to learn so much about the business of building a robots. This robot is a particularly good tool for this, as it combines the social aspect of children playing together, with the opportunity to learn so much about robotics, programming and engineering. A map with the names of these components would really bring the whole thing together beautifully. 


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

So...

What a great weekend we've had. We're all really grateful to http://redrobotics.co.uk/ for providing us with this Easter fun. We are now especially looking forward to the day when we can put our names down to have a robot of our own from their first commercial release. 

Thanks!






Friday, 6 January 2017

New image library launched


This image shows a piece of mechanical equipment that would have been used about 30 years ago (and still now, really) to study how plants respond to growing without gravity. The plant is being rotated so that the force of gravity constantly changes.

The photo was taken by M.B. Wilkins at the University of Glasgow. To see his other images visit the listings page.

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


Chlorophyllosophy Image Library



I have just launched a new little website which is the beginnings of a commercial plant science image library.

It's starting with my own images and with the images of M.B.Wilkins, the Emeritus Regius Professor of Botany at Glasgow University.

If you would like to see it, the site is called the Chlorophyllosophy Image Library.

If would can pass the message on to plant science and plant photographer friends that would be great. I would like to collect together lots of people's images if possible.

Thanks!