How to Configure a Digital Potentiometer (eg : MCP41100) as Rheostat or Potentiometer.

Digital Potentiometers are used for two purposes : One is as Rheostat mode and other one is Potentiometer mode. Lets see the pins of MCP41100 :

Here the potentiometer communicates with Arduino using SPI Protocol. By default, Arduino D10 is SS (CS bar), D11 is MOSI (SI) , D12 is MOSO, and  D13 is SCK. So connect the potentiometer pins to Arduino as mentioned mapping. Further :

Vss – GND

Vdd – Supply Voltage (2.7V to 5.5V)

PW0 – Output is taken

PA0 – To GND

PB0 – To Vdd ( For using as potentiometer) or To PW0 ( For using as Rheostat)

Now Circuit is ready. Time to move to programming.

Program : 

#include <SPI.h>  //Include SPI library

void setup()

{

  SPI.begin();  //Initialize SPI

  Serial.begin(9600);

  pinMode(ss, OUTPUT);  //Set Pin Direction ,Other SPI pins are     //configured automatically

}

void loop()

{

    for (int i=0; i<=255; i=i+1)  // This is because 8 bit Range

    {

      SetPot(0x11,i);

      delay(200);

    }

    delay(200);

    for (int i=255; i>=0; i=i-1)

    {

      SetPot(0x11,i);

      delay(200);

   }

  }

//Function to Set the Potentiometer

void SetPot(int regVal, int levelVal)

{

  digitalWrite(ss,LOW);

  SPI.transfer(reg);

  SPI.transfer(level);

  //Serial.print(level);

  digitalWrite(ss,HIGH);

}

Done.

Note 1 : If you have picked D10 as Slave Select you cannot name it as SS because by default it is named as SS. But you can select any pin for SS. You only need to set the declare and configure its pin mode.

Node 2 : If you want control voltage, You do not need a Digital Potentiometer because you can do it using Arduino only but if you need to get varying resistance use Digital Potentiometer.

How to Add a Python Program to Run in Boot of The Raspberry Pi Every Time

Say You want to add a python program to run when booting of Raspberry Pi without any log in even if power goes and then power comes. First to do this check your python program is working properly. So lets see how to do this :

Go to terminal and type the following : sudo nano /etc/rc.local

You will get a window of rc.local. There you type the command before exit 0 to run the python program. It will be as follows :

sudo python /home/pi/name_of_your_program.py

Here you should care about the directory where your program is. For example if the program  is in Desktop put the directory as correct.

Then you follow the simple saving of the rc.local.

Now you reboot your computer, you can see your program is running in the black window where you need not do anything.

But what is the drawback here is you cannot easily stop the program by simply using Ctrl+Z or Ctrl+C or with anything. You may thing it is bad but if you think as you are going to set up our product to a normal home user where he cannot do anything with just stopping using Ctrl+Z or with anything. So how can we do this ?

Yes it can be done with some modifications of the cmdline.txt file in the memory card. For this I come later. Easily you can do this when you switch of and switch on the Raspberry Pi, It will as you to recovery press “Shift”. Press Shift and you can see an option “Edit config”. Click on that you will get a window of Config Editor where you click cmdline.txt and put the following command at the end of previous commands :

init=/etc/sh      ( Note : Do not confuse with / and \)

Now you can Exit and you will will be in black window where you type the following command again :

sudo nano /etc/rc.config

you will get the rc.local window which you have edited before for adding auto start. Now remove the line and save it. Then you ave to shut down the Raspberry and start again.  To shut down the RPi sudo halt will or other sudo does not work. Type the following : poweroff -f

No power off and power on the RPi again and by pressing Shift remove the init=/etc/sh and exit. Now you can use RPi as normal one. That’s all done!

Note : If you want to edit the cmdfile.txt from accessing memory card, You cannot see the file if you are using it in windows. This is because of partition issues. So you have to put the memory card in any Linux operating systems or Mac.