Categories

Arduino and keyes RGB Led module example

This example was for an RGB Led display I saw. It contains 8 led’s, 3 control lines for the red, green and blue and also requires a 5v connection. The LEDs are common cathode types so a low (0v) switches them on and a high (5v) switches them off.

So a lot of pins required for an Arduino Uno out of the box. Its quite a niche little product but if you wanted a bar of RGB led’s this could be an effective solution

Here is a picture of the module

Connection

Vcc – Arduino 5v

D0 – D7 -> Connect these to Arduino pins 2 to 9

R, G, B -> Connect these to Arduino pins 10 to 12

Code

Simple example that cycles through the red, green and blue colours. You could add more colours, have different effects and so on.

void setup()
{
  //all pins are outputs and we are using 2 to 13
  for (int x =2;x<13;x++)
  {
    pinMode(x,OUTPUT);
    digitalWrite(x,HIGH);
  }
}
 
void loop()
{
   blue();
   cycle(); 
   green();
   cycle();
   red();
   cycle();
}
 
void blue()
{
  digitalWrite(12,LOW);
}
 
void green()
{
  digitalWrite(11,LOW);
}
 
void red()
{
  digitalWrite(10,LOW);
}
 
 
void cycle()
{
  for(int x=2;x<10;x++)
  {
    digitalWrite(x,LOW);
    delay(500);
    digitalWrite(x,HIGH);
  }
  digitalWrite(10,HIGH);
  digitalWrite(11,HIGH);
  digitalWrite(12,HIGH);
}

 

Links

The module comes in at the $5 mark
Full Color LED Module / SCM light water

Share

Arduino FreeRTOS example

Out of the box the Arduino IDE does not support multi-tasking effectively. the good news its easy to add support via a library to the Arduino IDE

Most operating systems appear to allow multiple programs or threads to execute at the same time. This is called multi-tasking. In reality, each processor core can only be running a single program at any given point in time. A part of the operating system called the scheduler is responsible for deciding which program to run when, and provides the illusion of simultaneous execution by rapidly switching between each program.

The scheduler in a Real Time Operating System is designed to provide a predictable execution pattern. Traditional real time schedulers, such as the scheduler used in FreeRTOS, achieve determinism by allowing the user to assign a priority to each thread of execution. The scheduler then uses the priority to know which thread of execution to run next. In FreeRTOS, a thread of execution is called a Task.

 

Installation

The following is for later versions of the Arduino IDE that support importing

Navigate to Sketch -> Include Library -> manage Libraries
Type in RTOS and Install the FreeRTOS library

Here is a screenshot of the Library Manager – you can see I have installed the library

Hardware

I decided to use the Easy Module shield as it has many analog inputs and digital outputs, so for a basic example its easy to use some of these. The FreeRTOS library does come with a couple of built in examples, this example will be similar

I will blink the red LED and take readings from the LDR and the pot on the board. Here is a FreeRTOS example, now this is probably overkill for something as basic as this but it gives you an idea of a fairly simple example.

In the real world working with microcontrollers, RTOS are popular so it doesn’t do any harm learning in a nice easy environment like the Arduino

 

Code

#include <Arduino_FreeRTOS.h>
 
// define three tasks for Blink & LDR & VR
void TaskBlink( void *pvParameters );
void TaskLDR( void *pvParameters );
void TaskVR( void *pvParameters );
 
// the setup function runs once when you press reset or power the board
void setup() 
{
 
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
 
  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  3
    ,  NULL );
 
 
  xTaskCreate(
    TaskVR
    ,  (const portCHAR *) "VR"
    ,  128  // Stack size
    ,  NULL
    ,  2  // Priority
    ,  NULL );
 
  xTaskCreate(
    TaskLDR
    ,  (const portCHAR *) "LDR"
    ,  128  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL );
 
}
 
void loop()
{
  // Empty. Things are done in Tasks.
}
 
/*---------------------- Tasks ---------------------*/
 
void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(12, OUTPUT);
 
  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(12, LOW);   // turn the LED on
    Serial.println("LED 12 on");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
    digitalWrite(12, HIGH);    // turn the LED off
    Serial.println("LED 12 off");
    vTaskDelay( 1000 / portTICK_PERIOD_MS );
  }
}
 
void TaskLDR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 1:
    int sensorValue = analogRead(A1);
    // print out the value you read:
    Serial.print("LDR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}
 
void TaskVR(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
 
 
  for (;;)
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.print("VR value = ");
    Serial.println(sensorValue);
    vTaskDelay(20);  // one tick delay (300ms) in between reads for stability
  }
}

 

If you open the Serial monitor you will be able to read the debug strings, adjust the pot, cover the LDR to change the values

Share

Run Arduino IDE as a portable version

One nice feature that has been introduced in the Arduino IDE is the ability to run the software as a portable version. The portable version will store the sketchbook, the libraries and the hardware folder locally where you want it.

This is handy for a number of scenarios but for me personally I like this as you could have a copy on a pen drive and move this from PC to PC and the big one is that having used the Arduino IDE for development on many platforms such as Arduino, Chipkit, ESP8266 and even ATTiny microcontrollers I have been looking for a way where you could just have a set of sketches and libraries just for that platform. Using this method you could have an installation for each one with just the supported core, libraries and sketches.

Its extremely easy to create the portable version

Download the compressed version of the Arduino IDE
Once download has been completed, extract the archive content to the location you want. e.g F:\ARDUINO
Open the extracted folder and create a new directory called portable in the root

The first time you open the IDE the structure will be populated and from that point on all the sketches, libraries and additional cores will be installed in the portable folder. You could copy the portable folder to another unzipped installation and all the settings from the first would be in place.

Share

Arduino and SI7021 Humidity and Temperature Sensor example

The Si7021 I2C Humidity and Temperature Sensor is a monolithic CMOS IC integrating humidity and temperature sensor elements, an analog-to-digital converter, signal processing, calibration data, and an I2C Interface. The patented use of industry-standard, low-K polymeric dielectrics for sensing humidity enables the construction of low-power, monolithic CMOS Sensor ICs with low drift and hysteresis, and excellent long term stability, it would be a great sensor to have on the roller. It would be able to measure everything before I go out for a ride around town.

Features

  • Relative Humidity Sensor:
    • Si7013/21: ± 3% RH (maximum) @ 0-80% RH
    • Si7020: ± 4% RH (maximum) @ 0-80% RH
    • Si7006: ± 5% RH (maximum) @ 0-80% RH
  • Temperature Sensor:
    • Si7013/20/21: ±0.4°C accuracy (maximum) @ -10 to +85°C
    • Si7006: ±1.0°C accuracy (maximum) @ -10 to +85°C
  • 0 to 100% RH operating range
  • Up to -40 to +125°C operating range
  • Wide operating voltage range (1.9 to 3.6V)
  • Low Power Consumption: 2.2µW average power at 3.3V and 1 sample per second
  • I2C host interface
  • Integrated on-chip heater
  • 3mm x 3mm QFN package
  • Excellent long term stability
  • Factory calibrated
  • Optional factory-installed filter/cover
    • Lifetime protection during reflow and in operation
    • Protects against contamination from dust, dirt, household chemicals and other liquids
    • AEC-Q100 automotive qualified (Si7013/20/21)

 

 

Connection

 

Must use 3v3 for Vin

Arduino Pins Module Pins
 3v3  Vin
 Gnd  Gnd
 SDA  SDA
 SCL  SCL

 

Code

#include 
 
const int ADDR =0x40;
int X0,X1,Y0,Y1,Y2,Y3;
double X,Y,X_out,Y_out1,Y_out2;
 
void setup()
{ 
  Serial.begin(9600);   
  Wire.begin();                                   
  delay(100);  
  Wire.beginTransmission(ADDR);
  Wire.endTransmission();                        
}
 
void loop()
{
  /**Send command of initiating temperature measurement**/
  Wire.beginTransmission(ADDR);
  Wire.write(0xE3);
  Wire.endTransmission();
 
  Serial.print("Temp");
  Serial.print("\t");
  Serial.println("Humidity");
 
  /**Read data of temperature**/
  Wire.requestFrom(ADDR,2);
 
  if(Wire.available()

 

Output

Open the serial monitor, you should see something like this

Temp    Humidity
23.12C    52.83%

Temp    Humidity
24.04C    53.13%

Temp    Humidity
26.28C    53.83%

Temp    Humidity
27.42C    54.57%

Temp    Humidity
28.27C    55.32%

Temp    Humidity
27.94C    56.11%

 

Links
Humidity Temperature Sensor Breakout Barometric Pressure SI7021 for Arduino GY-21

Share