BminiIO

From Biemme Italia Wiki
Jump to: navigation, search

Contents


The purpose of this library is to simplify the access to all the digital/analog inputs and outputs of the BMini shield.

Download and installation

The BMiniIO library can be downloaded directly from this wiki. Please select from the list below the library depending on which Arduino board you are using:

As with any other Arduino library, you can place it under the libraries directory of the Arduino installation. In case you are not familiar with that or you are not able to identify the correct directory, please take a look at this brief tutorial that explain where Arduino libraries must be placed.

Pre requisites

We suppose that you're already familiar with Arduino statements, Arduino IDE, and how Arduino MCU works in general. In case you're not, please refer to Arduino Getting Started tutorial or any good Arduino book. All the examples we will present, are compiled with Arduino 1.0.1.

Initialization

In order to use the library once installed, you have to import the library header file with the following include statement:

#include "BminiIO.h"

The include statement has to be at the top of the Arduino sketch. Indeed, add in the setup procedure of the sketch the following library initialization call :

void setup(){
  // bmini pins initialization
  bmini_init();
}

Please note that pins initialization through pinMode command is not necessary for the analog and digital I/O.

Once the library is initialized, in the main sketch loop you can use the library's functions for managing:

We will now walk through each of these and sketch some examples.

Digital Inputs

See the PCB Components Layout for details.

Digital inputs are located in the J5 connector plug. They are opto-iso­lated 24 V digital inputs. Please refer to the pin mapping of our wiki. The 5th pin is the common (0V). Each digital inputs is connected to a Led (LD3 to LD6) and once an input goes to 0V, the corresponding led will switch on. If you want to test your digital inputs, just take a wire and make a bridge between the common pin and one of the digital inputs.

In order to read the status of the digital inputs, just use the get_inputs function providing a pin number. The function will return 1 (or the constant HIGH) when the input is high, zero otherwise (or LOW respectively). The example that follows counts the number of times the digital input #1 is HIGH.

#include "BminiIO.h"
#define DIG_PIN 1
 
void setup(){
  // bmini I/O initialization
  bmini_init();
}
int count=0;
void loop(){
 
    if (get_inputs(DIG_PIN)==1){
    	count++;
    }
 
  delay(100);
}

Digital Outputs - Relays

See the PCB Components Layout for details.

Digital outputs are connected to 4 high quality 24VDC relays and fuses through J9-J12 connector plug. They work as NC-C-NO. The following block of code reads all the digital inputs and once one of them is HIGH, set HIGH the corresponding relay.

#include "BminiIO.h"
 
void setup(){
  // bmini I/O initialization  
  bmini_init();
}
 
int i;
void loop(){  
 
  for(i=1; i < 5; i++){
    if (get_inputs(i)==1){
      relay(i,HIGH);
    }else{
      relay(i,LOW);
    }
    /* or: 
    relay(get_inputs(i); 
    */
  }
  delay(100);
 
}

Note that the BminiIO library accepts indexes that start at 1.

Pulse with Modulation (PWM)

See the PCB Components Layout for details.

The two PWM pins are located in the connector J6. The following sketch writes analog values ranging from 0v to 5v to the first pwm pin using the function pwm. It accepts two parameters:

  1. the pwm pin
  2. the value to write (belonging to 0-255)
#include "BminiIO.h"
void setup(){
  // bmini I/O initialization   
  bmini_init();
}
 
unsigned int j=0;
 
void loop(){
 
  pwm(1,j);
 
  j+=5;
  if (j>255){
    //when the value exceeds 255, restart from 0
    j=0;
  }
  delay(100);
}

Analog Inputs

J7 (Leftmost) and J8 (Rightmost) connectors. See the BMini PCB Components Layout for details.

Analog inputs are very important when dealing with external sensors. You can access 4+4 analog inputs from connector J7-J8 (the number of available analog pins depends on the Arduino board you are using, check the pin mapping). Both RJ45 connectors have 5V and GND pins (pin 1 and pin 4 respectively) that can be used for power external sensors.

All the analog inputs can be used as digital input or ouputs. In the latter case, they act as 0-5V outputs. In any case, remember to initialize the pins on the setup procedure with the Arduino pinMode command.

The following is an example showing how to turn on/off the air conditioner when the temperature exceeds a threshold.

#include "BminiIO.h"
#define temperature_threshold 25.4
 
void setup(){
  // bmini I/O initialization 
  bmini_init();
 
}
 
void loop(){
 
	// Celsius temperature (TMP36 sensor)
	float tmp36C = readTemperatureC(1);
 
	// Fahrenheit temperature (TMP36 sensor)
	// float tmp36F = readTemperatureF(1);	
 
       //int val = analogReadPin(1); /*for reading pure analog value*/
 
	if (tmp36 > temperature_threshold)
		relay(1,HIGH);	//turn on Air conditioner
	else
		relay(1,LOW);  //turn off Air conditioner
 
	delay(10);
}

The functions involved are readTemperatureC and readTemperatureF. They both accept a parameter, i.e., the analog input to read and return the Celsius and Fahrenheit temperature value respectively. The conversion from the analog value is done by assuming to work with TMP36 temperature sensors or similar.

If you prefer to use other temperature sensors than the TMP36 one, just use the function analogReadPin instead of the readTemperatureC and readTemperatureF functions specifying an analog input pin. This function will give you a 10 bit precision integer number ranging [0-1024]. It is necessary to add your own algorithm that transforms the integer values to Celsius or Fahrenheit.

Dealing with float numbers are convenient and easy, but in systems with limited resources could increase the allocation memory needed for your program and slow down the MCU because of the high number of calculations involved. Working with integer numbers should be a natural solutions since analog inputs are treaded as discrete numbers by the MCU (using the ADC). If the analogReadPin function is not enough for your needs, you can directly use the Arduino's analogRead function, but in this case you must take care of controlling which pin to read. Please take a look at the pin mapping before attempting to read analog values.

Licensing

This documentation is licensed under the Creative Commons Attribution-ShareAlike License 3.0

Personal tools
Namespaces

Variants
Actions
Structure
Store