There are many ways to clean a Nikon D40 or other D-SLR camera. Once you learn the most common way to clean the lens and sensor, you can service your own camera. However, heed the tips and warnings before starting this project so you don't damage the camera.
Clean your work area so it is dust-free.
Remove the lens from camera by holding the base of the lens in one hand and the camera in the other. Turn the lens to the left and it should twist off the end of the camera.
Lock up the mirror by pushing the menu button, then select the "Setup" menu. Select the mirror lock-up button, and switch it to "On."
Open the shutter by turning the dial on top of the camera to Manual, "M" mode. Rotate the command wheel by your right thumb past the 30 second speed, until the screen reads "B" or Bulb.
Hold open the shutter to access the sensor.
Use the air blower to remove large debris from the camera sensor, without lowering the blower past the lens mount.
Use the air blower to blow off your sensor brush.
Swipe the sensor brush over the sensor from left to right just once and blow off the debris. Repeat this process if necessary. Blast the sensor and brush with air between each pass.
Remove the sensor swab from the packaging, and blow the end with the blower to ensure that its clean.
Put a couple of drops of cleaning solution on the swab so it is barley moist, not dripping wet. Tilt the swab in the direction you want to sweep the sensor, then pull the swab across the face of the sensor.
Close the shutter by putting your camera back in auto mode, and lower the mirror by going back into the set-up menu and turning the mirror lock-up button to "Off."
Put the lens or cap back on your camera.
Read through and understand how to clean your camera before beginning this project, as wasting time figuring it out while you have the sensor exposed allows more dust and debris to enter your camera. Make sure your camera has a fully charged battery or the mirror lock won't work. It is better to use an AC adaptor because if the battery fails the mirror will come down and shutter will automatically close, damaging your tools and camera. For small cleaning jobs, it may be enough just to use the air blower and sensor brush.
Cleaning a camera sensor is risky. If the sensor gets scratched or damaged the cost of repairs would be equivalent to a new camera. If you are not a camera expert, strongly consider taking your camera to a professional. Don't use compressed air cans as they are high-pressure and release moisture, which can damage the sensor. Use an air blower with a foot pump that allows you to control the air pressure. Never touch the end of the sensor swab, as oils from your hand can cause damage. Also, if the swab is too wet, moisture will seep into your camera.