Tag Archives: arduino

Arduino : How to put quotation marks in a string ?

In Arduino programming many times you will come with situations where you want to put double quotes in a string. For example sending AT command with double quotes. There many different methods let’s discuss one by one.

How do I put quotes in a string?

AT+CPMS=”SM”

Serial.println(“AT+CPMS=”SM””);  // This results in error

Continue reading Arduino : How to put quotation marks in a string ?

Arduino SPI Communication Example

Introduction

This tutorial describes how to set up and use the on-chip Serial Peripheral Interface (SPI) of the Arduino Board. Most AVR devices come with an on board SPI and can be configured according to requirements. This tutorial contains, theoretical background and the steps to configure the SPI to run in both master mode and slave mode.

A Serial Peripheral Interface (SPI) bus is a system for serial communication, which uses up to four conductors, commonly three. One conductor is used for data receiving, one for data sending, one for synchronization and one alternatively for selecting a device to communicate with. It is a full duplex connection, which means that the data is sent and received simultaneously. The maximum baud rate is higher than that in the I2C communication system.

General Description of the SPI

The SPI allows high-speed synchronous data transfer between the AVR and peripheral devices or between several AVR devices. On most parts the SPI has a second purpose where it is used for In System Programming (ISP).
The interconnection between two SPI devices always happens between a master device and a slave device. Compared to some peripheral devices like sensors, which can only run in slave mode, the SPI of the AVR can be configured for both master and slave mode. The mode the AVR is running in is specified by the settings of the master bit (MSTR) in the SPI control register (SPCR). Special considerations about the SS pin have to be taken into account for Multi Slave Systems.
The master is the active part in this system and has to provide the clock signal a serial data transmission is based on. The slave is not capable of generating the clock signal and thus can not get active on its own. The slave just sends and receives data, if the master generates the necessary clock signal. The master, however, generates the clock signal only while sending data. That means the master has to send data to the slave to read data from the slave.

SPI uses the following four wires −

  • SCK − This is the serial clock driven by the master.
  • MOSI − This is the master output / slave input driven by the master.
  • MISO − This is the master input / slave output driven by the master.
  • SS − This is the slave-selection wire.

The following functions are used. You have to include the SPI.h.

  • SPI.begin() − Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
  • SPI.setClockDivider(divider) − To set the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter of the frequency of the system clock (5 Mhz for the boards at 20 MHz).
  • Divider − It could be (SPI_CLOCK_DIV2, SPI_CLOCK_DIV4, SPI_CLOCK_DIV8, SPI_CLOCK_DIV16, SPI_CLOCK_DIV32, SPI_CLOCK_DIV64, SPI_CLOCK_DIV128).
  • SPI.transfer(val) − SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal.
  • SPI.beginTransaction(SPISettings(speedMaximum, dataOrder, dataMode)) − speedMaximum is the clock, dataOrder(MSBFIRST or LSBFIRST), dataMode(SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3).

We have four modes of operation in SPI as follows −

  • Mode 0 (the default) − Clock is normally low (CPOL = 0), and the data is sampled on the transition from low to high (leading edge) (CPHA = 0).
  • Mode 1 − Clock is normally low (CPOL = 0), and the data is sampled on the transition from high to low (trailing edge) (CPHA = 1).
  • Mode 2 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from high to low (leading edge) (CPHA = 0).
  • Mode 3 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from low to high (trailing edge) (CPHA = 1).
  • SPI.attachInterrupt(handler) − Function to be called when a slave device receives data from the master.

Now, we will connect two Arduino UNO boards together; one as a master and the other as a slave.

  • (SS) : pin 10
  • (MOSI) : pin 11
  • (MISO) : pin 12
  • (SCK) : pin 13

The ground is common. Following is the diagrammatic representation of the connection between both the boards −

Let us see examples of SPI as Master and SPI as Slave.

Arduino SPI as Master

Master unit sends hello world data to slave unit.

#include <SPI.h>

void setup (void) {
   Serial.begin(115200); //set baud rate to 115200 for usart
   digitalWrite(SS, HIGH); // disable Slave Select
   SPI.begin ();
   SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}

void loop (void) {
   char c;
   digitalWrite(SS, LOW); // enable Slave Select
   // send test string
   for (const char * p = "Hello, world!\r" ; c = *p; p++) 
   {
      SPI.transfer (c);
      Serial.print(c);
   }
   digitalWrite(SS, HIGH); // disable Slave Select
   delay(2000);
}

Arduino SPI as Slave

Slave unit waits for data as soon as data is arrived process variable becomes true, indicating there is data in buffer. in main loop we read this buffer and send to serial terminal.

#include <SPI.h>
char buff [50];
volatile byte indx;
volatile boolean process;

void setup (void) {
   Serial.begin (115200);
   pinMode(MISO, OUTPUT); // have to send on master in so it set as output
   SPCR |= _BV(SPE); // turn on SPI in slave mode
   indx = 0; // buffer empty
   process = false;
   SPI.attachInterrupt(); // turn on interrupt
}

ISR (SPI_STC_vect) // SPI interrupt routine 
{ 
   byte c = SPDR; // read byte from SPI Data Register
   if (indx < sizeof buff) {
      buff [indx++] = c; // save data in the next index in the array buff
      if (c == '\r') //check for the end of the word
      process = true;
   }
}

void loop (void) {
   if (process) {
      process = false; //reset the process
      Serial.println (buff); //print the array on serial monitor
      indx= 0; //reset button to zero
   }
}

Results

Open serial monitor of slave, you will see “Hello, World”.

Arduino Error avrdude: stk500_getsync(): not in sync: resp=0x00

Many new people find this error avrdude: stk500_getsync(): not in sync: resp=0x00 while uploading program to board.

Before we start to conclude the error first know how arduino works.

What is Arduino IDE?

Arduino IDE is a special software running on your system that allows you to write sketches (synonym for program in Arduino language) for different Arduino boards. The Arduino programming language is based on a very simple hardware programming language called processing, which is similar to the C language.

What is the programming language for Arduino?

In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++).

How Arduino Program upload works?

Arduino board is basically consists of main three components.

  1. ATmega328p or similar
  2. USB to Serial Converter
  3. 5V Power supply

In most cases program is uploaded using usb cable or using external usb to serial converter.

arduino with bootloader

Error Reason 1: Blank ATmega328p Chip

When u buy fresh micrcontroller such as ATmega328p from market. It is completely blank. To program using arduino you need USB-ASP (SPI based programmer) shown below.

Reason 1: You are trying to program using serial of arduino to a blank controller.

Flashing Arduino boot-loader to fresh ATmega328p.

  1. Connect usbasp programmer to arduino.
  2. Select Board from Tools>>Boards>>Arduino UNO
  3. Select programmer usbAsp from Tools>>Programmer>>USBASP
  4. Click on Tools >> Burn Bootloader

After uploading bootloader you can use serial port / usb to program your arduino.

Error Reason 2: Power Supply

When using external USB2Serial converter, May be you have not connected +5V to microcontroller or Incorrect connections of Rx TX.

Error Reason 3: External Device on Rx Tx Line

You have connected external device on Rx Tx pin i.e. Arduino Pin 0 and Pin 1.

Remove or Disconnect any circuit present on Rx Tx lines and try again.

Error Reason 4: Wrong Selection of Port

You have selected incorrect Serial Port. Go to Tools>>Ports Menu and select proper port.

or Incorrect board selection.

Error Reason 5: Missing Drivers

If you will not find correct serial port, check that you have installed correct USB to Serial converter or Arduino Drivers. for windows look for any yellow sign (indicates error) on Serial ports.

For linux see this

Common solutions to correct the error

  • Disconnect and reconnect the USB cable.
  • Press the reset button on the board.
  • Restart the Arduino IDE.
  • Make sure you select the right board in Tools ► Board ►, e.g. If you are using the Duemilanove 328, select that instead of Duemilanove 128. The board should say what version it is on the microchip.
  • Make sure you selected the right port in Tools ► Serial Port ►. One way to figure out which port it is on is by following these steps:
    1. Disconnect the USB cable.
    2. Go to Tools ► Serial Port ► and see which ports are listed (e.g. COM4 COM5 COM14).
    3. Reconnect the USB cable.
    4. Go back to Tools ► Serial Port ►, and see which port appeared that wasn’t there before.
  • Make sure digital pins 0 and 1 do not have any parts connected, including any shields.

Error Reason 6: Damaged controller

Unfortunately, it can also mean that you burned your microcontroller. Were you doing anything dangerous right before you tried to upload a new sketch?

Error Reason 7: Using External USB to Serial converter

When using external USB to serial converter you need to connect RTS pin of usb2serial converter to reset pin of micro-controller through a 0.1uF (104) capacitor.

You need four pins from external serial converter RX, TX, GND and RTS —||— with 0.1uF capacitor connect one terminal of capacitor to RTS pin and another to reset pin of micro-controller.

Error Reason 8: Windows 10 permission

Try running Arduino IDE in administrator mode.

Error Reason 9: External Crystal is missing or faulty

Check that Most of the Arduino boards use 16MHz or 8MHz crystal, If you are trying with fresh chip, this is common mistake. Use 16MHz crystal for ATmega328p with Arduino UNO boot-loader

These are the most common mistakes that cause error avrdude: stk500_getsync(): not in sync: resp=0x00

RCWL-0516 Doppler Radar Sensor Interface with Arduino

In this tutorial we interface RCWL-0516 Human body induction switch module Intelligent sensor (Radar Motion sensor) with Arduino.

This mini doppler radar motion sensor module is equipped with supporting DC 4-28V wide voltage. It will automatically continuously output the high level TTL signal when there is motion. 360 degree no blind angle detection and maximum 7m sensing distance. It is perfect for DIY microwave motion sensor light switch, human sensor toys, smart security devices, etc.Microwave Motion Sensor

Continue reading RCWL-0516 Doppler Radar Sensor Interface with Arduino