Sunday 17 January 2016

Blink LED on NodeMcu DevKit ver1.0 / ESP8266 12E, Lua, WIFI, IOT,

Introduction:

Wi-Fi is an essential bit of kit for any Internet of Things (IoT) DIY projects, but our favorite Arduino doesn’t come with Wi-Fi, and adding in a Wi-Fi shield can increase the costs substantially.
Meet the Arduino Killer: ESP8266 (also known as NodeMCU) was originally marketed as a low cost Wi-Fi add-on for Arduino boards, until the hacker community realized you could cut the Arduino out of the equation entirely.



In less than a year, the ESP8266 has rocketed in popularity, and is now so well supported and developed that if you’re currently using Arduino, you need to stand up and take note.
There’s quite a few models of ESP8266 around now, but we recommend this one: ESP-12E (also known as NodeMCU 2.0). This board includes the serial driver needed to program the chip, and has a built-in power regulator, as well as lots of IO pins.This NodeMCU Devkit is available from our eBay Store.

Getting Started with ESP8266-12E:

Using Micro usb to standard usb cable Connect  ESP8266-12E  to your PC. we are using windows 7.

Please wait and allow the windows to install the USB driver as shown below.


NodeMCU is connected to COM5 . This can also be verified from Device Manager.



Method 1: Arduino IDE

This is the easier of the two methods requiring only Arduino IDE

  • Install the latest Arduino from the Arduino website.
  • Start Arduino and open Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform 

Selecting the NodeMCU baord from Tools Menu





Restart Arduino IDE and select blink Example for ESP8266 and upload.



Results:




Method 2: Flashing the Firmware:

This Method Involves flashing the firmware and then uploading the lua code via ESPlorer.

download the latest firmware from the following link

or the direct link

we are using the firmware: nodemcu_float_0.9.6-dev_20150704.bin

Download and run the flasher software from github

we are using the 64 bit version of the flasher.

Select the firmware file by clicking on the gear icon.



Then press the Flash(F) button to start the firmware flash process.




Once finished  press the reset button on the NodeMCU.

Running Blink led program:

Now download and run the ESPlorer IDE from the following link
connect to NodeMCU by pressing the Open button on the top of right panel.


After successful connection  some message and a prompt is displayed.


Now we are ready to upload our first program "Blink.lua" to NodeMCU. Press [Save to ESP] button to upload, the red led on ESP will start flashing with 1 sec interval. Please note 1,000,000 us = 1 sec.

"Blink.lua" code:

              while 1 do
              gpio.write(0,gpio.HIGH)
              tmr.delay(1000000)
              gpio.write(0,gpio.LOW)
              tmr.delay(1000000)

              end



Results:





WIFI Scanner:


Setup:


OLED -> NodeMcu

-SDA     ->   D5
-SCL      ->   D6
-VCC      ->  3.3V
-GND     ->   GND

Code:


wifi.setmode(wifi.STATION) --Set mode to STATION so he chip can receive the SSID broadcast
function init_OLED(sda,scl) --Set up the u8glib lib
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(sla)
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end
init_OLED(5,6) --Run setting up
tmr.alarm(0,3000,1,function() --A timer, which used to run the following program 
    wifi.sta.getap(function(t) 
         disp:firstPage()
         repeat
            lines = 0
            for k,v in pairs(t) do
                disp:drawStr(0,lines * 11,k.." "..v:sub(3,5).."dbi") --Print the data out
                lines = lines + 1
            end
        until disp:nextPage() == false
    end)
end)

Results:





Saturday 16 January 2016

Hello World 0.96 inch 128X64 I2C OLED, on Arduino Uno, using u8glib library

Introduction:

 OLED (Organic Light-Emitting Diode) is a self light-emitting technology composed of a thin, multi-layered organic film placed between an anode and cathode. In contrast to LCD technology, OLED does not require a backlight. OLED possesses high application potential for virtually all types of displays and is regarded as the ultimate technology for the next generation of flat-panel displays.
Blue and White Character OLEDs are available from our eBay Store.

Features:

It's a 0.96" 128X64 OLED, I2C (or IIC) interface, with SSD1306 driver, 3.3/5V compatible.

1. A simplified manufacturing process compared to TFT-LCD
2. Self-emitting light, in contrast to the required backlight for TFT-LCD
3. High luminosity 
4. Lightweight and thin (less than 2 mm)
5. Capable of wide viewing angles>160
6. Low operating voltage and power consumption
7. Quick response (< second level)
8. Wide range of operating temperatures (-30c to 70c)

Specifications:


  •          High resolution: 128 * 64
  •       Viewing angle:> 160 degree
  •       Supports many control chip: Fully compatible with Arduino, 51 Series, MSP430 Series, STM32 / 2, CSR IC, etc.
  •       Ultra-low power consumption: full screen lit 0.08W
  •       Voltage: 3V ~ 5V DC
  •       Working Temperature: -30 c ~ 70 c
  •       Module Size: 27.0MM * 27.0MM * 4.1MM
  •       I2C/IIC Interface, need 2 IO only.
  •       Driver IC: SSD1306
  •       Character Colour: White



Hardware Setup:

There are 4 pin on the OLED module, GND, VCC, SCL and SDA. Connect the oled module to your Breadboard. Get four male to male dupont jumper wires and Connect to Arduino as shown in the following image and table:


OLED -> Arduino

-SDA     ->   A4
-SCL      ->   A5
-VCC      ->  3.3V

-GND     ->   GND



Software Setup:

u8glib (Universal Graphics Library for 8 Bit Embedded Systems) is a  graphics library with support for many different displays.
To install u8glib go to manage libraries in Arduino IDE menu:

and from the list of available libraries install u8glib by oliver library.


find and load the HelloWorld example in the library.


In the HelloWorld, only un-comment the following constructor to make it work:
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);

or use the following code:

Arduino Code:

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 10, "Hello World!");
  u8g.drawStr( 50, 35, "By");
  u8g.drawStr( 10, 56, "Alictronix!");
}

void setup(void) {
  // flip screen, if required
  // u8g.setRot180();
 
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}

void loop(void) {
  // picture loop
  u8g.firstPage(); 
  do {
    draw();
  } while( u8g.nextPage() );
 
  // rebuild the picture after some delay
  delay(50);
}

Results:



White Character Display:





Blue Character Display:





2004 20x4 I2C LCD modules with HD44780-compatible interface + PCF8574 I2C backpacks for Arduino

Introduction

Like the more common version of I2C 1602 LCD, this LCD also has blue backlight. It has 4 rows of 20 characters. With this I2C interface LCD module, you will be able to realize data display via only 2 wires. If you already has I2C devices in your project, this LCD module actually cost no more resources at all. It is fantastic for Arduino based project. This LCD is available from our eBay Store.

Features

·       Interface: I2C
·       I2C Address: 0x27 or 0x3F
·       Pin Definition : GND,VCC,SDA,SCL
·       Back lit (Blue with White char color)
·       Supply voltage: 5V
·       Pcb Size : 60mm×99mm
·       Contrast Adjust :  Potentiometer
·       Backlight Adjust : Jumper

 

I2C ADDRESS:

There are a few versions of 2004 Lcd available with different I2c address e.g. 0x20, 0x27, 0x3F.
If not sure , I2C address can be verified by running  I2C scanner sketch from the Arduino playground.

For our LCD the address was verified as 0x27.


Hardware Setup:

Get four male to female dupont jumper wires and connect the LCD module to your Arduino as shown in the following image and table. Then connect your Arduino to the computer via USB:




Software Setup

The next step is to download and install the Arduino I2C LCD library for use with the backpack. First of all, rename the “LiquidCrystal” library folder in your Arduino libraries folder. We do this just to keep it as a backup.
If you’re not sure where your library folder can be found – it’s usually in your sketchbook folder, whose location can usually be found in the Arduino IDE preferences menu:



Next, visit  https://bitbucket.org/fmalpartida/new-liquidcrysta…  and download the latest file, currently we’re using v1.3.4. Expanding the downloaded .zip file will reveal a new “LiquidCrystal” folder – copy this into your Arduino libraries folder.

Now restart the Arduino IDE if it was already running – or open it now. To test the module we have a demonstration sketch prepared, simply copy and upload the following sketch:

Arduino Code:

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
  lcd.begin(20,4);
 // lcd.setBacklightPin(3,POSITIVE);
  //lcd.setBacklight(HIGH);// initialize the lcd
 
}
void loop()
{
  lcd.home ();                   // go home
  lcd.print(" Hello World !");
  lcd.setCursor(8,1);
   lcd.print("By");
   lcd.setCursor(6,2);
   lcd.print("Alictronix!");
   lcd.setCursor(2,3);
   lcd.print("++++++++++++++++");
   delay(1000);
}

Results:

After a few moments the LCD will be initialized and start to display "Hello world", – for example:

If the text isn’t clear, or you just see white blocks – try adjusting the contrast using the potentiometer on the back of the module. You can permanently turn off the backlight by removing the physical jumper on the back of the module.

Friday 15 January 2016

Android App controlled 4WD RC Car Kit Assembly Tutorial










1. The Kit

This kit is available from our eBay store.
It comes with the following  parts:

4 x Geared motors + 4 x Tyres/wheels + 4 x Motor fixings
4 x black + 4 x red motor cables.
2 x  transparent acrylic glass plate 250 x 160 x 3mm
1 x L298N motor driver
1 x  Arduino UNO328 controller board (CH340 usb driver installation needed.)
1 x HC-06 Bluetooth module
1 x Mini breadboard
2 x Battery holder (6V , 4 x AA / batteries not included)
1 x Battery holder (9V ,  batteries not included)
4 x motor speed encoding disks (Use with IR optocoupler sensors which are not included)
30 x 20 cm Dupont jumpers cable, 10 each(M-M,M-F,F-F)
1 x USB cable for arduino 
6 x Copper pillar ( 30 mm ),  1 x Screws kit 
Some Extras: on/off toggle mini switch, extra screws and nuts, dc jack for Arduino battery, tie cables etc


2. Connect the motors


Connect the motors and wheels as shown in the  figure 1.  Solder the red and black wires as shown. These correspond to the positive and negative of the motors/battery terminals. If you connect the red and black to the +ve and -ve of the battery , all the motors/ wheels should spin in the same direction. If any wheel spins in a different direction swap the wires on the motor terminals to correct the spin.

Figure 1: Connecting wheels and motors




2. Connect L298n motor board
Connect the red and black wires from two right side motors to +/-ve terminal on L298n  board which are numbered 1/2 respectively.

Figure 2: L298N motor driver pin outs

Similarly Connect the red and black wires from two left side motors to +/-ve terminal on L298n  board which are numbered 13/14 respectively.



Figure 3 Connecting red and black wires to L298n driver.



4. Connect L298n motor control pins to Arduino
Attach top plate and screw Arduino to it. Next, connect your power supply - the positive to pin 4 on the module and negative/GND to pin 5. If you supply is up to 12V you can leave in the 12V jumper (point 3 in the image above) and 5V will be available from pin 6 on the module. This can be fed to your Arduino's  5V pin to power it from the motors' power supply. Don't forget to connect Arduino GND to pin 5 on the module as well to complete the circuit. 
Now you will need six digital output pins on your Arduino, two of which need to be PWM (pulse-width modulation) pins. PWM pins are denoted by the tilde ("~") next to the pin number, as shown in Fig 4:

Figure 4 Arduino Pinouts

Finally, connect the Arduino digital output pins to the driver module. In our example we have two DC motors, so digital pins D9, D8, D7 and D6 will be connected to pins IN1, IN2, IN3 and IN4 respectively. Then connect D10 to module pin 7 (remove the jumper first) and D5 to module pin 12 (again, remove the jumper). 


L298n
Arduino
7
D10
IN1(8)
D9
IN2(9)
D8
IN3(10)
D7
IN4(11)
D6
12
D5

Table 1 Arduino connections to L298N

The motor direction is controlled by sending a HIGH or LOW signal to the drive for each motor (or channel). For example for motor one, a HIGH to IN1 and a LOW to IN2 will cause it to turn in one direction, and  a LOW and HIGH will cause it to turn in the other direction.
However the motors will not turn until a HIGH is set to the enable pin (7 for motor one, 12 for motor two). And they can be turned off with a LOW to the same pin(s). However if you need to control the speed of the motors, the PWM signal from the digital pin connected to the enable pin can take care of it. The range of speed possible of your hardware, set the PWM values between zero to 255.

Figure 5 Connect to Arduino


5. Connect bluetooth hc-06 module to Arduino


Table 2 and Fig show how you should connect the Bluetooth module to your Arduino.

HC-06
Arduino
VCC
3.3v
GND
GND
TXD
RXD
RXD
TXD

Table 2 Arduino connections to Hc-06

Figure 6: hc-06 connections to Arduino

You can also connect the VCC to 5v though that is not advised, especially if you are using the setup for long.
The HC-06 acts as a serial port through which you can send and receive data. So using a serial terminal or a Bluetooth customized application on your computer or phone, you can control and monitor your project. you can use Teraterm  for windows or Bluetooth Termial for android device as the serial terminal.
Before, uploading the code to the Arduino, disconnect the HC-06 module, since it shares the tx/rx pins and will interfere with the upload. Connect it back once the code has been uploaded successfully.

The full setup is shown Fig 7 .  I have used 6v (4xAA) to power Arduino and separate 12 V (8xAA) to power the motors board.



Figure 7: hc-06 connection to arduino




6. Arduino code:

// This code is for the bluetooth  controlled  arduino car.
// Bluetooth is always enabled and available . The password is 1234.
// Android car control app can be found here:
// https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=en
//Pin assignments and global variables per function. Customize if needed
//*******Pin assignments Motor board ********************

const int MotorRightPWM = 10;
const int MotorRight1 = 9;
const int MotorRight2 = 8;

const int MotorLeftPWM = 5;
const int MotorLeft1 = 7;
const int MotorLeft2 = 6;
int iSpeed = 255; //speed, range 0 to 255
const int LedPin=13;
//*****Bluetooth signals**************************************************
char val; //stores received character. Needs to be global to perform continuous movement

//*********General SETUP: activate pins***********************************
void setup() {
 //start receiving serial infor
 Serial.begin(9600);
 //motor connections
 pinMode(MotorRight1, OUTPUT);  //
 pinMode(MotorRight2, OUTPUT);  //
 pinMode(MotorLeft1,  OUTPUT);  //
 pinMode(MotorLeft2,  OUTPUT);  //
 pinMode(MotorRightPWM, OUTPUT); //enable for right side motor
 pinMode(MotorLeftPWM, OUTPUT); //enable for right side motor
}
//**************Movement functions******************************
void advance(int d) { //go straight
 digitalWrite(MotorRight1, HIGH);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, HIGH);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void right(int d) { //turn right (single wheel)
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void left(int d) {//turn left(single wheel)
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void turnR(int d) {//turn right (two wheels)
 digitalWrite(MotorRight1, HIGH);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}
void turnL(int d) {//turn left (two wheels)
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, HIGH);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);
 delay(d * 10);
}
void stopp(int d) { //stop
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, LOW);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, LOW);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}
void back(int d) { //go back
 digitalWrite(MotorRight1, LOW);
 digitalWrite(MotorRight2, HIGH);
 digitalWrite(MotorLeft1, LOW);
 digitalWrite(MotorLeft2, HIGH);
 analogWrite(MotorRightPWM, iSpeed);
 analogWrite(MotorLeftPWM, iSpeed);  
 delay(d * 10);
}


//*************************Bluetooth functionality***********************
//Bluetooth commands
void bluetoothCommand() {
 if (Serial.available()) { //check if bluetooth command available
   val = Serial.read();
   Serial.write(val);
 }
 if (val == 'F') { // Forward
   advance(10);
 }
 else if (val == 'S') { // Stop Forward
   stopp(10) ;
   val = Serial.read(); //read value again, otherwise can't continu with infrared
 }
 else if (val == 'B') { // Backwards
   back(10);
 }
 else if (val == 'R') { // Right
   turnL(10);
 }
 else if (val == 'L') { // Left
   turnR(10);
 }
 else if (val == 's') { // Stop, not used though
   stopp(10 ) ;
 }
 else if (int(val) >= 49 && int(val) <= 57) { //set speed
   iSpeed = (int(val)-48)*26;
   Serial.println("Speed set to: " + iSpeed);
 }
 else if (val == 'q') { //set speed
   iSpeed = 255;
   digitalWrite(LedPin,HIGH);   
   Serial.println("Speed set to: " + iSpeed);  
 }
 else if (val == 'W') {
   digitalWrite(LedPin,HIGH);
 }
 else if (val == 'w') {
   digitalWrite(LedPin,LOW);
 }
}
//**************************************MAIN LOOP***************************************
void loop() {
 //bluetooth commands
 bluetoothCommand();
}

7. Android App

The link to Download the app from google play store is:
A screen shot of the app is shown in Fig 8. The app has both accelerometer and button control functionality.
Figure 8: Bluetooth RC Controller