In this instructable, I will show how to control a robot with infrared TV remote. I'm sure you all are using remotes to operate electronics, but have you ever wonder how they work?
Infrared signals are beams of light that operate at a wavelength that cannot be seen with a naked eye. When you press a button from your remote, the LED on the remote will turnoff and on in an unique pattern sending the signal to the receiver in the TV. The received signals pluses are converted to electrical signals that are further converted into data in the receiver's electronics.
For this, we are only concerned on receiving the signals. For the best results use any Sony remote.
Step 2: Materials
This project is an update to my previous instructable.
Steps on how to install a library can be found here. This project heavily relies on coding.
Now we need to select the HEX codes of the remote to use it to send directions to our robot. Connect the IR receiver to the arduino as shown in the schematics. Then, open the serial monitor and press four remote buttons you wanted to use to control the movements of the robot ( I used the up/down arrows, you could use numbers instead). Jot down the HEX code.
<p>int receiverpin= 11;<br>#include <IRremote.h></p><p><irremote.h><irremoteint.h><irremotetools.h>IRrecv irrecv(receiverpin); // create instance of irrecv
decode_results results;</irremotetools.h></irremoteint.h></irremote.h></p><p>void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
}</p><p> void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.print(results.value, HEX); // display IR code on the Serial Monitor
Serial.print(" ");
irrecv.resume(); // receive the next value
}
}</p>
I got the following Hex codes:
Up arrow= 9CB47
Down= 5CB47
Left= 3CB47
Right= DCB47
Test ends here. Full setup will be explained in the schematics and code section.
Step 4: Schematics
In addition to my previous project's schematics, this setup should be accompanied with it.
Unlike previous project, there is a slight change in the pins that are connected to the motor are as follows:
Right side
Pin 1 --------------------------------- 4 of Arduino
If you find the robot tracks are rotating in opposite directions, that is clockwise and anti-clockwise or vice-versa, reverse the pins (4-7 to 7-4) or numbers in the code.
see the full code in next step.
Step 5: Code
<p>int receiverpin= 11;<br>#include <IRremote.h><irremote.h></irremote.h></p><p>IRrecv irrecv(receiverpin); // create instance of irrecv
decode_results results;</p><p>void setup()
{
Serial.begin(9600);
//left motor
pinMode(4,OUTPUT); //
pinMode(7,OUTPUT);
pinMode(6,OUTPUT); //pwm
//right right
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(9,OUTPUT);//pwm
irrecv.enableIRIn(); // start the receiver
}</p><p>void onLED(int pin, int voltage)
{
digitalWrite(pin, voltage);
}</p><p>void goForward(int duration, int pwm)
{
digitalWrite(4,HIGH);
delay(duration);//100
digitalWrite(7,LOW);
analogWrite(6,pwm);//right motor
digitalWrite(13,HIGH);
delay(duration);
digitalWrite(12,LOW);
analogWrite(9,pwm);// right motor
}</p><p>void fullReverse(int duration, int pwm)
{
digitalWrite(4,LOW);
delay(duration);
digitalWrite(7,HIGH);
analogWrite(6,pwm);
digitalWrite(13,LOW);
delay(duration);
digitalWrite(12,HIGH);
analogWrite(9,pwm);// right motor
}</p><p>void fullStop(int duration, int pwm)
{
digitalWrite(4,LOW);
delay(duration);
digitalWrite(7,LOW);
analogWrite(6,pwm);//
digitalWrite(13,LOW);
delay(duration);
digitalWrite(12,LOW);
analogWrite(9,pwm);// right motor
}</p><p>void turnLeft(int duration, int pwm)
{
digitalWrite(13,LOW);
delay(duration);
digitalWrite(12,HIGH);
analogWrite(9,pwm);// right motor
digitalWrite(4,HIGH);
delay(duration);
digitalWrite(7,LOW);
analogWrite(6,pwm);
}</p><p>void turnRight( int duration, int pwm)
{
digitalWrite(13,HIGH);
delay(duration);
digitalWrite(12,LOW);
analogWrite(9,pwm);// right motor
digitalWrite(4,LOW);
delay(duration);
digitalWrite(7,HIGH);
analogWrite(6,pwm);
}
void translateIR()
// takes action based on IR code received
// uses Sony IR codes
{
switch(results.value)
{
case 0x9CB47: goForward(100,120); // (x,y), adjust the y values for speed
break;</p><p> case 0x5CB47: fullReverse(100,100);
break;</p><p> case 0x3CB47: turnLeft(100,120);
break; </p><p> case 0xDCB47: turnRight(100,120);
break; </p><p> default:fullStop(100,0);
break;</p><p> }
}</p><p> void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
onLED(2,255); // on led when the signal is being recieved
translateIR();
for (int z = 0 ; z < 2 ; z++) // ignore the repeated codes
{
irrecv.resume(); // receive the next value
}
}
else
{
onLED(2,0); // off if the signal is not being recived
}
}</p>
In this article, we will delve into the process of installing and running the SLM(small language model) Gemma2 on the Single Board Computer (SBC) Raspberry Pi 5 using the Ollama runtime framework.