Categories

Lowpower library example for Arduino

There are some situations where you need to save power when you are creating battery powered projects, the lowpower library is one solution (we will look at others later)

 

Code

You will need to install the Lowpower library – https://github.com/rocketscream/Low-Power

This example will flash an LED connected to pin 4 on and off but does not use the delay function

#include "LowPower.h"
int led = 4;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(led,HIGH);
// ATmega328P, ATmega168
LowPower.idle(SLEEP_1S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
digitalWrite(led,LOW);
LowPower.idle(SLEEP_2S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
}
Share

PS2 JOYSTICK SHIELD V2.0 code example

In this code example we will take a look at the PS2 JOYSTICK SHIELD V2.0.

The X-axis of the joystick is connected to pin A0 of Arduino, the Y-axis to pin A1, power supply to 5V and ground to GND.

There are four buttons on the PS2 JOYSTICK SHIELD . D3 should be connected to digital pin 3; D4 to digital pin4; D5 to digital pin 5; D6 to digital pin 6. The other small button is reset button.
Generally, PS2 rocker can be used for controlling small vehicles. It is mainly comprised of two 10K potentiometers and a button switch. The five ports are VCC, X, Button, Y and GND.

 

Code

char button0=3;
char button1=4; 
char button2=5; 
char button3=6;
char joybutton=2;
 
void setup(void)
{
  //set all as inputs
  pinMode(joybutton, INPUT); 
  pinMode(button0, INPUT);      
  pinMode(button1, INPUT);     
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
 
  //set all high
  digitalWrite(joybutton, HIGH);
  digitalWrite(button0, HIGH);  
  digitalWrite(button1, HIGH);  
  digitalWrite(button2, HIGH);
  digitalWrite(button3, HIGH);
 
  Serial.begin(9600); 
}
 
void loop(void)
{
  Serial.print(analogRead(0));
  Serial.print(",");
  Serial.print(analogRead(1));
  Serial.print(",");
 
  //Read the value of the buttons and print it on the serial port.
  Serial.print(digitalRead(button0)); 
  Serial.print(digitalRead(joybutton));    
  Serial.print(digitalRead(button0)); 
  Serial.print(digitalRead(button1));  
  Serial.print(digitalRead(button2));  
  Serial.println(digitalRead(button3)); 
 
  delay(1000);
}

 

Links
PS2 JOYSTICK SHIELD V2.0 for Arduino

Share

Arduino relay shield controlled by remote control example

In this example we will connect a 4 channel relay shield to an Arduino Uno and control it via an infrared remote control. We will use keys 1 to 4 to control relay 1 to 4 and also use key 0 to switch off relays.

We will code this so that if you press the 1 key the Relay will be activated, if you then press the 1 key again the relay will be switched off.

 

Image

Here is a picture of what we created, the Infrared reciever was a module that I purchased, link at the bottom

Code

The code needs the IRremote remote library to be installed. Then you need to find the codes for the various buttons on the remote control

#include <IRremote.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume();
}
}

Now upload the code above and open the serial monitor, when you press each button you should see the hex code for the keys you want to use, here is an example

0xFF30CF
0xFF18E7

Lets look at some of the code, remember that Relay 1 is pin 7 and so on

int RelayControl1 = 7;    // Digital Arduino Pin used to control the motor
int RelayControl2 = 6;  
int RelayControl3 = 5;  
int RelayControl4 = 4;

We then define some variables to hold the state of each relay, here is an example
boolean state1 = true;

The next piece of interesting piece of code is the handling of each button press, you can see we detect the hex code, if its match we then check the value of the state1
variable, if its true then the relay is switch on and the state is changed. This of course means that if the button is pressed again the value of the state 
variable will be false and we switch the relay off, and set the state vartiable to true.

      case 0xFF30CF:  
        Serial.println("1");
        if(state1==true)
        {
          digitalWrite(RelayControl1,HIGH);// NO1 and COM1 Connected (LED on)  
          state1=false; //change the state
        }  
        else
        {
          digitalWrite(RelayControl1,LOW);// NO1 and COM1 disconnected (LED off)  
          state1=true; //change the state
        }      
        break;


Here is t eh complete code example
#include <IRremote.h>
 
int RECV_PIN = 3;
int RelayControl1 = 7;    // Digital Arduino Pin used to control the motor
int RelayControl2 = 6;  
int RelayControl3 = 5;  
int RelayControl4 = 4;  
boolean state1 = true;
boolean state2 = true;
boolean state3 = true;
boolean state4 = true;
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()  
{
    Serial.begin(9600);
  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}
 
 
void loop()  
{
  if (irrecv.decode(&results))
  {
    switch(results.value)
    {
      case 0xFF30CF:  
        Serial.println("1");
        if(state1==true)
        {
          digitalWrite(RelayControl1,HIGH);// NO1 and COM1 Connected (LED on)  
          state1=false; //change the state
        }  
        else
        {
          digitalWrite(RelayControl1,LOW);// NO1 and COM1 disconnected (LED off)  
          state1=true; //change the state
        }      
        break;
 
      case 0xFF18E7:  
        Serial.println("2"); 
        if(state2==true)
        {
          digitalWrite(RelayControl2,HIGH);// NO2 and COM2 Connected (LED on)
          state2=false;
        }
        else
        {
          digitalWrite(RelayControl2,LOW);// NO2 and COM2 disconnected (LED off)  
          state2=true; //change the state          
        }
        break;
 
      case 0xFF7A85:  
        Serial.println("3"); 
        if(state3==true)
        {
          digitalWrite(RelayControl3,HIGH);// NO3 and COM3 Connected (LED on)
          state3=false;
        }
        else
        {
          digitalWrite(RelayControl3,LOW);// NO3 and COM3 disconnected (LED off)  
          state3=true;           
        }
        break;
 
      case 0xFF10EF: 
        Serial.println("4"); 
        if(state4==true)
        {
          digitalWrite(RelayControl4,HIGH);// NO4 and COM4 Connected (LED on)
          state4=false;
        }
        else
        {
          digitalWrite(RelayControl4,LOW);// NO4 and COM4 disconnected (LED off)  
          state4=true;          
        }
        break;
 
      case 0xFF6897:  
        Serial.println("OFF"); 
        digitalWrite(RelayControl1,LOW);
        digitalWrite(RelayControl2,LOW);
        digitalWrite(RelayControl3,LOW);
        digitalWrite(RelayControl4,LOW);
        state1 = true;
        state2 = true;
        state3 = true;
        state4 = true;
        break;
 
      default :
          Serial.println("Invalid Option");
        break;
    }
  delay(250);
  irrecv.resume();
  }
}

 

 

Links
Expansion board 5V 4 – way relay module Relay Shield

KEYES Infrared receiver sensor module for arduino

Share

4 Wheel robot car basic example

I have been looking to do some work with robots and purchased a few different different robot car kits. There are many options available, some have 2 wheels, some have 4 wheels. You can also choose various different options to do all of the work from a Raspberry Pi, Arduino, STM32 and many more.

In this example we will take a basic 4wd smarty car chassis and add some basic parts to it, there will be some code examples demonstrating various functions.

Lets first take a look at a typical smart car chassis, this is a 4 wheel version

As you can see you basically get the frame, motors and wheels and all required screws and nuts and instructions on how to put this all together. there are options which will give you an Arduino, motor driver and various other sensors but this option allows you to build your own spec of car but obviously is a slightly more advanced option

My first decision was to choose how I was going to control and drive the motors, being the DC type and also because there were 4 of them I decided to go with an Arduino Uno and a motor control shield, this is a motor shield

I wired M1 and M2 to the front 2 motors and M3 and M4 to the rear motors.

 

Layout

This is a basic layout drawn up in fritzing to give you the general idea of what I am building

Code

First of all I needed a simple test program to verify I had wired the motor correctly.

This requires the Adafruit motor library – https://github.com/adafruit/Adafruit-Motor-Shield-library

#include <AFMotor.h>
 
AF_DCMotor motor(1, MOTOR12_64KHZ); 
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
 
  motor.setSpeed(200);     // set the speed to 200/255
 
  Serial.print("forwards");
  motor.run(FORWARD);      // turn it on going forward
  delay(1000);
 
  Serial.print("backwards");
  motor.run(BACKWARD);      // turn it on going forward
  delay(1000);
 
  Serial.print("stop");
  motor.run(RELEASE);      // stopped
  delay(1000);
}
 
void loop() {
 
}

Now a basic example which will move forward, backwards and stop

#include <AFMotor.h>
 
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR34_1KHZ); // create motor #3, 1KHz pwm
AF_DCMotor motor4(4, MOTOR34_1KHZ); // create motor #4, 1KHz pwm
 
void setup() 
{
  Serial.begin(9600);//set up Serial library at 9600 bps
  Serial.println("Motor test!");  
  MotorSpeed(200);
  AllForward();
  delay(1000);
  AllBackward();
  delay(1000);
  AllStop();
  delay(1000);
}
 
void loop() {
 
}
 
void MotorSpeed(int Speed)
{
  motor1.setSpeed(Speed);     // set the speed to 200 (255 max)
  motor2.setSpeed(Speed); 
  motor3.setSpeed(Speed); 
  motor4.setSpeed(Speed);
}
 
void AllForward()
{
  Serial.print("forward");  
  motor1.run(FORWARD);      // turn it on going forward
  motor2.run(FORWARD); 
  motor3.run(FORWARD); 
  motor4.run(FORWARD); 
}
 
void AllBackward()
{
  Serial.print("backward");  
  motor1.run(BACKWARD);      // turn it on going forward
  motor2.run(BACKWARD); 
  motor3.run(BACKWARD); 
  motor4.run(BACKWARD); 
}
 
void AllStop()
{
  Serial.print("stop");
  motor1.run(RELEASE);      // turn it on going forward
  motor2.run(RELEASE); 
  motor3.run(RELEASE); 
  motor4.run(RELEASE); 
}

 

Links
4WD Smart Robot Car Chassis Kits for arduino with Speed Encoder New

AliExpress.com Product – Free shipping 4WD Smart Robot Car Chassis Kits for arduino with Speed Encoder New

Share